C# 使用Azure AD OpenIdConnect和自定义中间件的ASP NET核心身份验证中的Cookie过期
当通过OpenIdConnect身份验证模型使用Azure AD对我的.NET核心应用程序进行身份验证时,我目前正在努力设置cookie/auth令牌上的超时 正在通过以下方式在ConfigureServices方法中设置登录方案:C# 使用Azure AD OpenIdConnect和自定义中间件的ASP NET核心身份验证中的Cookie过期,c#,azure,authentication,openid-connect,asp.net-core-1.0,C#,Azure,Authentication,Openid Connect,Asp.net Core 1.0,当通过OpenIdConnect身份验证模型使用Azure AD对我的.NET核心应用程序进行身份验证时,我目前正在努力设置cookie/auth令牌上的超时 正在通过以下方式在ConfigureServices方法中设置登录方案: services.AddAuthentication(options => options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme); 然后,我将按照以下方式设
services.AddAuthentication(options => options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme);
然后,我将按照以下方式设置配置:
app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
CookieName = "MyCookie",
ExpireTimeSpan = TimeSpan.FromHours(2)
});
app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions()
{
Authority = authorityUri.AbsoluteUri,
ClientId = azureOptions.ClientId,
ClientSecret = azureOptions.ClientSecret,
ResponseType = OpenIdConnectResponseTypes.CodeIdToken,
Events = new OpenIdConnectEvents()
{
OnAuthorizationCodeReceived = async context =>
{
await aAuthenticateMiddleware.OnAuthenticate(context, logger);
}
}
});
app.UseMiddleware<aAuthenticateMiddleware>();
这一切都可以正常工作,并正确验证用户,等等,但这是在发出认证令牌(和cookie),到期时间为15分钟,忽略我尝试设置的2小时到期时间
我一直在参考来自aspnet/security存储库的GitHub的最新源代码示例。。。。然而,这些都没有提到任何关于覆盖默认到期日的内容
我发现的大多数示例仍然引用旧的AspNet库,而不是AspNetCore库
一些文章建议使用SignInAsync并将persistent设置为True,这样就可以遵守ExpireTimeSpan,但是在调用它时会抛出一个“不支持的异常”。或许Azure AD不支持SignInAsync
有人知道如何实现这一点吗?
UseOpenIdConnectAuthentication
setUseTokenLifetime=false
注意:链接https://github.com/aspnet/Security/tree/dev/samples/OpenIdConnect.AzureAdSample
返回404。
var authenticationProperties = new AuthenticationProperties() { RedirectUri = context.Request.Path.Value ?? "/" };
authenticationProperties.AllowRefresh = false;
authenticationProperties.IssuedUtc = DateTime.Now;
authenticationProperties.ExpiresUtc = DateTime.Now.AddHours(2);
await context.Authentication.ChallengeAsync(
authenticationManager.IdentityProvider.AuthenticationScheme,
authenticationProperties,
ChallengeBehavior.Automatic
);