设置CookieDomain导致登录失败 [英] Set CookieDomain results in login failure

查看:432
本文介绍了设置CookieDomain导致登录失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置Cookie域在我的网站,因为我要在我的子域共享验证cookie。

I've had to set the Cookie Domain on my site because I have to share the authentication cookie across my subdomains.

当我设置此属性的网站仍然有效登录用户,但奇怪的事情发生了。登录操作似乎是可以的,因为登入返回成功,但是当我尝试将用户重定向到受保护的动作,他将自动重定向回到登录页面。我怀疑某些片丢失。

When I set this Attribute the site still works to login the user but something weird happens. The login action seems to be OK because the signin returns success but when I try to redirect the user to a secured action he is automatically redirected back to the login page. I suspect that some piece is missing.

我已经创建使用需要鉴定的基本MVC模板一个简单的测试项目,只是改变了CookieDomain如下图所示。

I've created a simple test project using the basic MVC template with Authentication and just changed the CookieDomain as shown below.

ConfigureAuth (Startup.Auth.cs)

ConfigureAuth (Startup.Auth.cs)

        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login"),
            Provider = new CookieAuthenticationProvider
            {
                // Enables the application to validate the security stamp when the user logs in.
                // This is a security feature which is used when you change a password or add an external login to your account.  
                OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                    validateInterval: TimeSpan.FromMinutes(30),
                    regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
            },
            CookieDomain = "localhost" // ****** here is my change

        });            

正如我所说的应用程序的其余部分与默认code,并在成功返回下面的行code的结果。

As I said the rest of application is with the default code and the below line code results in a success return.

登录(AccountController.cs)

Login (AccountController.cs)

var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);

我已经成功试图设置CookieDomain为.localhost,我也试图设置Cookie路径为/\".

I've unsuccessfully tried to set the CookieDomain to ".localhost" and I also tried to set Cookie Path to "/".

推荐答案

RDay的评论指出,正确的答案,我在这里发帖只是为了保持东西放在一起。

RDay's comment pointed to the right answer and I'm posting here just to keep the things together.

如果你在本地主机则CookieDomain必须设置为。这个简单,但很难发现。

If you are in a localhost then the CookieDomain must be set to "". That simple but hard to discover.

        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login"),
            Provider = new CookieAuthenticationProvider
            {
                // Enables the application to validate the security stamp when the user logs in.
                // This is a security feature which is used when you change a password or add an external login to your account.  
                OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                    validateInterval: TimeSpan.FromMinutes(30),
                    regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
            },
            CookieDomain = "" // here is the answer since we are running on localhost

        });

这篇关于设置CookieDomain导致登录失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆