【问题标题】:Why is cookie's expiration date is 'Session' when using Owin为什么使用 Owin 时 cookie 的过期日期是“会话”
【发布时间】:2018-10-11 04:41:13
【问题描述】:

我的网络应用程序是 MVC5。我在登录时调用 IdentityServer4 应用程序的 url 来验证用户身份。 这是我的应用程序中 Startup 类的 ConfigureAuth 方法

public void ConfigureAuth(IAppBuilder app)
    {
        JwtSecurityTokenHandler.InboundClaimTypeMap = new Dictionary<string, string>();            

        var authority = LayeredConfiguration.GetValue("HydraInsuranceWeb-UserManagement-Authority");
        var redirectUri = LayeredConfiguration.GetValue("HydraInsuranceWeb-UserManagement-RedirectUri");

        app.UseCookieAuthentication(new CookieAuthenticationOptions {
            AuthenticationType = "Cookies",
            SlidingExpiration = false,
            ExpireTimeSpan = System.TimeSpan.FromMinutes(2),
            CookieName = "MyTestCookie"
        });

        app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
        {
            Authority = authority,
            ClientId = AuthConstants.InsuranceWebClientId,
            Scope = "openid profile user.management hydra.eventhistory.api",
            RedirectUri = redirectUri,
            ResponseType = "code id_token",

            SignInAsAuthenticationType = "Cookies",
            UseTokenLifetime = false,

            Notifications = new OpenIdConnectAuthenticationNotifications
            {
                SecurityTokenValidated = n =>
                {
                    try
                    {
                        var transformedHydraIdentity = new HydraIdentityBuilder(n.AuthenticationTicket.Identity)
                                .AllowSecurityAdmin()
                                .IncludeRoleProfiles()
                                .IncludeIdToken(n.ProtocolMessage.IdToken)
                                .IncludeStandardClaims()
                                .Build();

                        n.AuthenticationTicket = new Microsoft.Owin.Security.AuthenticationTicket(
                            transformedHydraIdentity,
                            n.AuthenticationTicket.Properties);
                    }
                    catch (Exception ex)
                    {
                        n.HandleResponse();
                        n.Response.Redirect("/Error/NoAuthorization");

                        DiagnosticService.Writer.AddError("Authentication Error", ex);
                    }

                    return Task.FromResult(0);
                },
            }
        });
    }        

登录后,cookie 的过期时间始终是“Session”,而不是当前时间加上 2 分钟。

但我的期望是 cookie 的过期时间是特定的日期时间,应该是当前时间加上 2 分钟。如果用户在 2 分钟内没有操作,则跳转到登录页面。

有人知道这个问题吗?请告诉我如何调查或调试以了解 cookie 的过期时间更改的原因。

还有 2 个 cookie:.AspNet.CookiesMyTestCookie。哪个cookie用于验证用户?

【问题讨论】:

  • 您明确将身份验证 cookie 名称设置为 MyTestCookie,因此这是您的身份验证 cookie。

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


【解决方案1】:

登录时需要将IsPersistent设置为True

AuthenticationManager.SignIn(new AuthenticationProperties{ IsPersistent = true, ExpiresUtc = DateTimeOffset.UtcNow.AddMinutes(30)}, userIdentity);

【讨论】:

    猜你喜欢
    • 2012-10-02
    • 2011-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 2015-03-31
    • 2022-10-04
    相关资源
    最近更新 更多