首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自定义OWIN CookieAuthenticationProvider在第一次/冷引导时失败

自定义OWIN CookieAuthenticationProvider在第一次/冷引导时失败
EN

Stack Overflow用户
提问于 2014-05-08 18:30:58
回答 1查看 3.3K关注 0票数 11

我们有一个自定义cookie auth提供程序,它将auth cookie设置为像.domain.com这样的主机名,而不是domain.commy.domain.com。我们这样做是为了让cookie跨所有子域和域工作。它很简单,如下所示。

问题

在应用程序冷启动后的第一次尝试中,cookie仍然带有域my.domain.com (我们的登录在my.domain.com上),尽管在执行下面的SubdomainCookieAuthentication代码后将其设置为.domain.com (用断点检查)。在随后的登录尝试中,cookie主机名是可以的。

问题

我怎样才能解决这个问题,这样即使在第一次尝试时,它也能工作?

自定义曲奇

代码语言:javascript
复制
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

代码语言:javascript
复制
public void ConfigureAuth(IAppBuilder app) 
{
    app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
        LoginPath = new PathString("/Account/Login"),
        Provider = new SubdomainCookieAuthentication()
    });
}
EN

回答 1

Stack Overflow用户

发布于 2015-09-09 00:55:39

我也遇到了同样的问题,Cookie域在第一次尝试时没有使用ResponseSignIn方法进行设置。我能够通过将Owin库更新为3.x并使用新的CookieManager设置域来解决这个问题。从这篇文章中找到了这个解决方案:

EndRequest stage?

代码语言:javascript
复制
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中注册

代码语言:javascript
复制
app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        ...
        CookieManager = new ChunkingCookieManagerWithSubdomains(), 
        ...
    }
);
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23549802

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档