当我们提供“/identity”路径时,身份服务器 3 记住我不工作

Identity server 3 remember me not working when we give "/identity" path

我对这个问题做了很多研究和阅读,最后发现这个问题与身份服务器有关url。我们已将 "/Identity" 分配给路径 (app.Map("/identity", idsrvApp =>),但记住我功能不起作用。如果我们删除它有效。由于该应用程序正在生产中并且有许多客户端依赖于此 url 更改它并使其工作并不容易。

有没有其他方法可以让它发挥作用?

这是身份服务器设置

public void Configuration(IAppBuilder app)
    {
        app.UseCors(CorsOptions.AllowAll);
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {

            AuthenticationType = "Cookies",
            ExpireTimeSpan = new TimeSpan(0, 30, 0),
            SlidingExpiration = true
        });

        app.Map("/identity", idsrvApp =>
        {
            var corsPolicyService = new DefaultCorsPolicyService()
            {
                AllowAll = true
            };
            var idServerServiceFactory = new IdentityServerServiceFactory();

            idServerServiceFactory.ConfigureUserService("Context");
            idServerServiceFactory.CorsPolicyService = new
                Registration<IdentityServer3.Core.Services.ICorsPolicyService>(corsPolicyService);
            // use custom ViewService
            idServerServiceFactory.ViewService = new Registration<IViewService, CustomViewService>();
            idServerServiceFactory.ScopeStore = new Registration<IScopeStore, ScopeStore>();
            idServerServiceFactory.ClientStore = new Registration<IClientStore, ClientStore>();
            var options = new IdentityServerOptions
            {
                Factory = idServerServiceFactory,
                SiteName = "Login",
                IssuerUri = ConfigurationManager.AppSettings["issuerUri"],
                PublicOrigin = ConfigurationManager.AppSettings["Origin"],
                SigningCertificate = LoadCertificate(),
                AuthenticationOptions = new IdentityServer3.Core.Configuration.AuthenticationOptions()
                {
                    CookieOptions = new CookieOptions()
                    {
                        AllowRememberMe = true,
                        SecureMode = CookieSecureMode.Always,
                        RememberMeDuration = TimeSpan.FromDays(30),
                        SlidingExpiration = true
                    },
                    EnablePostSignOutAutoRedirect = true,
                    LoginPageLinks = new List<LoginPageLink>(){
                        new LoginPageLink() {
                             Href = "forgotpassword",
                             Text = "Reset Your Password",
                             Type = "forgotpassword"
                        }
                   }
                }
            };
            idsrvApp.UseIdentityServer(options);
        });
    }
    X509Certificate2 LoadCertificate()
    {
        return new X509Certificate2(
            string.Format(@"{0}\certificates\idsrv3test.pfx",
            AppDomain.CurrentDomain.BaseDirectory), "idsrv3test");
    }

以下是 Brock Allen 和 LeastPrivileage 已回复但未提供解决方案的一些帖子。这些问题都有同样的问题。

https://github.com/IdentityServer/IdentityServer3/issues/3693

https://github.com/IdentityServer/IdentityServer3/issues/2426

终于找到答案了。 当我们为我们的 Identity Server 路由提供 "/identity" 时,会为路径 "/identity" 生成 cookie,这就是 remember me 不起作用的原因。

要解决此问题,我们必须为 CookieOptions 提供 cookie 路径 Path = "/",如下所示

  app.Map(
            "/identity",
            coreApp =>
                {
                    var factory =
                        new IdentityServerServiceFactory()
                            .UseInMemoryClients(Clients.Get())
                            .UseInMemoryScopes(Scopes.Get());
                    factory.ViewService = new Registration<IViewService, IdentityCustomViewService>();

                    factory.Register(new Registration<CustomIdentityDbContext>(resolver => HttpContext.Current.GetOwinContext().Get<CustomIdentityDbContext>()));

                    factory.Register(new Registration<CustomUserManager>(resolver => HttpContext.Current.GetOwinContext().GetUserManager<CustomUserManager>()));

                    factory.Register(new Registration<CustomAspNetIdentityUserService>(x => new CustomAspNetIdentityUserService(x.Resolve<CustomUserManager>())));

                    factory.Register(new Registration<UserManager<CustomIdentityUser, int>>(x => x.Resolve<CustomUserManager>()));

                    factory.UserService = new Registration<IUserService>(x => x.Resolve<CustomAspNetIdentityUserService>());

                    coreApp.UseIdentityServer(
                        new IdentityServerOptions
                        {
                            SiteName = "Identity Server",
                            SigningCertificate = Cert.Load(),
                            Factory = factory,
                            RequireSsl = true,
                            AuthenticationOptions = new IdentityServer3.Core.Configuration.AuthenticationOptions
                            {
                                IdentityProviders= ConfigureIdentityProviders,
                                EnablePostSignOutAutoRedirect = true,
                                CookieOptions = new IdentityServer3.Core.Configuration.CookieOptions()
                                {
                                    AllowRememberMe = true,
                                    SecureMode = CookieSecureMode.Always,
                                    RememberMeDuration = TimeSpan.FromDays(30),
                                    IsPersistent = false,
                                    Path = "/"
                                },
                            }
                        });

                });