ASP.Net Identity(2.2.1)Cookie在同一主机上的不同webapps上

时间:2015-08-20 08:36:45

标签: asp.net-mvc cookies asp.net-identity

我需要在用户切换应用程序时更改cookie的名称。为此,我更改了Cookie名称选项,例如Asp.Net Identity and cookie names

但不幸的是,没有cookie被发送到浏览器,所以用户从不进行身份验证,我不知道为什么,我没有想法...

这是ConfigureOAuth中的代码:

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
    LoginPath = new PathString("/Home/Login"),                
    Provider = new CookieAuthenticationProvider
    {                    
        OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<IMS_UserManager, IMS_User, int>(
            validateInterval: TimeSpan.FromMinutes(30),
            regenerateIdentityCallback: (manager, user) => user.GenerateUserIdentityAsync(manager),
                getUserIdCallback: (claim) => int.Parse(claim.GetUserId()))
    },
    CookieDomain = ConfigurationManager.AppSettings["IdentityCookieURL"].ToString()
    CookieName = ".Asp.Net." + AuthenticationCookieName,
    CookieHttpOnly = true
});

我做错了什么? (AuthenticationCookieName是&#34; IMS&#34;,所以没有花哨的字符。)

谢谢,

PS:WebApps都在本地主机中但具有不同的端口(在IDE中运行,但在生产中它们将在内部网服务器上,具有相同的配置)...

PS2:问题确实存在于CookieName = ".Asp.Net." + AuthenticationCookieName,行,因为当我删除它时,它就像一个魅力...... HttpContext.Current.User.IsAuthenticated始终返回false,并且使用chrome,“localhost&#39; ...

中没有Cookie

1 个答案:

答案 0 :(得分:0)

假设您的意思是您在localhost:xxx上运行这些网站,我不相信当您尝试将Cookie域设置为完全不同的时(例如,如果可能的话,可以考虑安全漏洞)。

您应该将Cookie域设置为您的基本域,并在您要共享的所有应用中执行相同的操作,例如:

CookieDomain = ConfigurationManager.AppSettings["IdentityCookieURL"].ToString()

因此在生产中你可以将应用程序设置值设置为ims.com,但是在dev中将其设置为localhost(你可以在环境中使用web.config转换来实现这种差异)