为什么在使用Owin时cookie的有效期是'Session'
问题描述:
我的web应用程序是MVC5。我打电话IdentityServer4应用程序的URL登录时进行身份验证的用户。 下面是启动类的方法ConfigureAuth在我的应用为什么在使用Owin时cookie的有效期是'Session'
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的有效期都为“会议”,而不是目前时间加2分钟。
但我的期望是cookie的到期日期是一个特定的日期时间,它应该是当前时间加2分钟。如果用户在2分钟内未运行,请跳转到登录页面。
有没有人知道这个问题?请告诉我如何进行调查或调试,以了解为什么cookie的验证更改。
还有2个饼干:.AspNet.Cookies
和MyTestCookie
。哪个cookie用于认证用户?
答
您需要设置IsPersistent
到True
在登录时。
AuthenticationManager.SignIn(new AuthenticationProperties{ IsPersistent = true, ExpiresUtc = DateTimeOffset.UtcNow.AddMinutes(30)}, userIdentity);
您明确设置身份验证cookie的名称为'MyTestCookie',所以这是你的身份验证的cookie。 –