【问题标题】:Sharing .AspNet.ApplicationCookie between two MVC projects在两个 MVC 项目之间共享 .AspNet.ApplicationCookie
【发布时间】:2018-05-10 15:27:32
【问题描述】:

我有一个 Web 项目来登录我们的用户。它让用户登录,创建“.AspNet.ApplicationCookie”cookie,然后重定向到页面“~/Views/../Redirect.cshtml”,该页面将根据一些规则选择网站用户将是什么重定向到。很简单,一个“redirecting...”消息和一个 setTimeout 函数。

问题是...网站 2 无法识别网站 1 创建的 cookie,只是将用户移回网站 1。

一些信息: 1 - 在本地运行两个站点 2 - 站点 1 和 2 在同一个服务器上运行在同一个域中,只是端口不同。 3 - 两个站点共享相同的 ApplicationUserManager.Create 调用,因为我将此类移至域项目。

Startup.cs如下:

public void ConfigureAuth(IAppBuilder app)
        {
            // Configure the db context, user manager and signin manager to use a single instance per request
            app.CreatePerOwinContext(AccountDbContext.Create);
            app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
            app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);

            // Enable the application to use a cookie to store information for the signed in user
            // and to use a cookie to temporarily store information about a user logging in with a third party login provider
            // Configure the sign in cookie
            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, DefaultAuthenticationTypes.ApplicationCookie))
                }
            });
            app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

            // Enables the application to temporarily store user information when they are verifying the second factor in the two-factor authentication process.
            app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5));

            // Enables the application to remember the second login verification factor such as phone or email.
            // Once you check this option, your second step of verification during the login process will be remembered on the device where you logged in from.
            // This is similar to the RememberMe option when you log in.
            app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);
        }

【问题讨论】:

    标签: asp.net-mvc redirect cookies


    【解决方案1】:

    原来问题出在应用程序池上。共享相同 cookie 的所有站点都需要在相同的应用程序池上运行。一旦我将它们全部设置为使用相同的,它就可以正常工作了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-27
      • 2019-03-29
      • 2014-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多