自定义OWIN CookieAuthenticationProvider在第一次/冷启动时失败

11
我们有一个自定义的cookie身份验证提供程序,将auth cookie设置为像.domain.com这样的主机名,而不是domain.commy.domain.com。我们这样做是为了使cookie在所有子域和域之间工作。如下所示,它非常简单。
问题:
在应用程序冷启动后的第一次尝试中,即使在执行下面的SubdomainCookieAuthentication代码之后(使用断点检查),cookie仍然带有my.domain.com的域名(我们的登录在my.domain.com上)。在随后的登录尝试中,cookie主机名是正确的。
问题:
如何修复此问题,以便即使在第一次尝试时也能正常工作?
代码:
自定义cookie auth
public class SubdomainCookieAuthentication : CookieAuthenticationProvider
{
    public override void ResponseSignIn(CookieResponseSignInContext context)
    {
        // We need to add a "." in front of the domain name to 
        // allow the cookie to be used on all sub-domains too
        var hostname = context.Request.Uri.Host;
        // works for www.google.com => google.com
        // will FAIL for www.google.co.uk (gives co.uk) but doesn't apply to us
        var dotTrimmedHostname = Regex.Replace(hostname, @"^.*(\.\S+\.\S+)", "$1");
        context.Options.CookieDomain = dotTrimmedHostname;            
        base.ResponseSignIn(context);
    }
}

这是在Owin启动类中进行初始化的,具体步骤如下:

类:Startup

文件:App_start\Startup.Auth.cs

public void ConfigureAuth(IAppBuilder app) 
{
    app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
        LoginPath = new PathString("/Account/Login"),
        Provider = new SubdomainCookieAuthentication()
    });
}

你曾经解决过这个问题吗? - dampee
好像是个 bug,是时候去 GitHub 上报告问题了。 - Akash Kava
1个回答

3

我遇到了与使用ResponseSignIn方法时Cookie Domain在第一次尝试中未设置的相同问题。 我通过将Owin库更新为3.x并使用新的CookieManager来设置域名来解决了这个问题。 在此帖子中找到了这个解决方案:

Owin如何能够在Application_EndRequest阶段之后设置Asp.Net Identity身份验证cookie?

public class ChunkingCookieManagerWithSubdomains : ICookieManager
{
    private readonly ChunkingCookieManager _chunkingCookieManager;

    public ChunkingCookieManagerWithSubdomains()
    {
        _chunkingCookieManager = new ChunkingCookieManager();
    }

    public string GetRequestCookie(IOwinContext context, string key)
    {
        return _chunkingCookieManager.GetRequestCookie(context, key);
    }

    public void AppendResponseCookie(IOwinContext context, string key, string value, CookieOptions options)
    {
        options.Domain = context.Request.Uri.GetHostWithoutSubDomain();
        _chunkingCookieManager.AppendResponseCookie(context, key, value, options);
    }

    public void DeleteCookie(IOwinContext context, string key, CookieOptions options)
    {
        options.Domain = context.Request.Uri.GetHostWithoutSubDomain();
        _chunkingCookieManager.DeleteCookie(context, key, options);
    }
}

public static class UriExtensions
{
    public static string GetHostWithoutSubDomain(this Uri url)
    {
        if (url.HostNameType == UriHostNameType.Dns)
        {
            string host = url.Host;
            if (host.Split('.').Length > 2)
            {
                int lastIndex = host.LastIndexOf(".");
                int index = host.LastIndexOf(".", lastIndex - 1);
                return host.Substring(index + 1);
            }
            else
            {
                return host;
            }
        }

        return null;
    }
}

然后,在Startup.Auth.cs中注册它。
app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        ...
        CookieManager = new ChunkingCookieManagerWithSubdomains(), 
        ...
    }
);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接