Asp.net

Asp.Net Identity - 在執行時設置 CookieDomain

  • April 28, 2018

如果我想從 Request.Url 或儲存在我的數據庫中的某些設置中提取此值,如何在執行時在 CookieAuthenticationOptions 中設置 CookieDOmain?

我想支持子域,但也支持多租戶,每個租戶都有不同的域。

目前已配置,我無權訪問其中任何一個。

保羅

您可以指定自己的 cookie 提供程序:

CookieAuthProvider myProvider = new CookieAuthProvider();
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
  AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
  LoginPath = new PathString("/Account/Login"),
  Provider = myProvider
});

要麼實現你自己的,要麼簡單地從現有的提供者繼承:

public class CookieAuthProvider : CookieAuthenticationProvider
{
   public override void ResponseSignIn(CookieResponseSignInContext context)
   {
     //Alter you cookie options
     //context.CookieOptions.Domain  =  "www...";      
     base.ResponseSignIn(context);
   }
}

和 implement ResponseSignIn,當端點在轉換為 cookie 之前提供了登錄資訊時呼叫它。通過實施此方法,可以更改進入票證的聲明和額外資訊。

您將收到一個CookieResponseSignInContext,它公開CookieOptions了可以在ResponseSignIn呼叫期間替換或更改的屬性。

Katana 項目的程式碼參考:

引用自:https://stackoverflow.com/questions/22989920