【问题标题】:Change the domain in a cookie (ASP.Net and Microsoft.Owin.Security)更改 cookie 中的域(ASP.Net 和 Microsoft.Owin.Security)
【发布时间】:2017-05-24 10:04:54
【问题描述】:

我使用 Microsoft.Owin.Security、Microsoft.Owin.Security.OpenIDConnect 和 Microsoft.Owin.Security.Cookies 库。它工作正常,我可以创建一个安全 cookie。 但在安全 cookie 中是域AAA.de。如何将 cookie 中的域更改为 .AAA.de

这是我用来登录用户的代码。

public void SignIn()
{
    if (!Request.IsAuthenticated)
    {
        HttpContext.GetOwinContext().Authentication.Challenge(
            new AuthenticationProperties(
                new Dictionary<string, string>
                {
                    {Startup.PolicyKey, Startup.SignInPolicyId}
                })
                {
                    RedirectUri = Redirect,
                }, OpenIdConnectAuthenticationDefaults.AuthenticationType);
    }
}

感谢您的帮助。

【问题讨论】:

标签: asp.net-mvc-3 owin.security


【解决方案1】:

可以使用自定义 Cookie 提供程序配置 cookie 域 - 这通常配置为应用程序启动过程的一部分 - 您可能还有一个 App_Start 文件夹,其中包含 Startup.Auth.cs 类(如果您'已经从典型的基础项目开始。

你的提供者看起来像:

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

然后您可以通过以下方式从您的启动类中调用它:

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

很大程度上基于this answer 到“Asp.Net Identity - 在运行时设置 CookieDomain”

【讨论】:

    猜你喜欢
    • 2011-06-22
    • 1970-01-01
    • 2018-06-14
    • 2011-01-11
    • 2016-08-15
    • 1970-01-01
    • 2011-03-18
    • 2015-07-04
    • 1970-01-01
    相关资源
    最近更新 更多