在ASP中使用Azure AD身份验证更新会话.. NET MVC项目



我已在ASP中配置了Azure AD身份验证。. NET MVC项目,使用OpenId Connect。身份验证工作,但问题是,60分钟后会话不再有效。在我的应用程序中,用户空闲很长一段时间是很常见的,并且应该能够继续工作而不必重新登录。这就是我在Startup.auth.cs中设置身份验证的方法:

app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions {
CookieManager = new SystemWebCookieManager()
});
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
ClientId = clientId,
Authority = Authority,
PostLogoutRedirectUri = postLogoutRedirectUri,
Notifications = new OpenIdConnectAuthenticationNotifications()
{
AuthorizationCodeReceived = (context) => 
{
return System.Threading.Tasks.Task.FromResult(0);
}
}
});

我已经尝试添加会话刷新逻辑,建议在https://www.cloudidentity.com/blog/2016/07/25/controlling-a-web-apps-session-duration-2/。这种方法的问题是登录窗口不能显示在框架中。

如何保持会话有效,直到用户关闭浏览器窗口?这似乎是一个常见的问题,应该有一些共同的解决方案,但我找不到一个。

您可以在类之前的' new OpenIdConnectAuthenticationOptions '部分的代码中添加下面突出显示的参数:-

app.SetDefaultSignInAsAuthenticationType
(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions {
CookieManager = new SystemWebCookieManager()});
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
ClientId = clientId,
Authority = Authority,
PostLogoutRedirectUri = postLogoutRedirectUri,
Notifications = new OpenIdConnectAuthenticationNotifications()
{
AuthorizationCodeReceived = (context) => 
{
return System.Threading.Tasks.Task.FromResult(0);
}
UseTokenLifetime = false
}
});'

认证会话的生存期(例如,cookie)应该与认证令牌的生存期相匹配。问题是您需要延长AAD中的令牌生存期,默认设置为一小时。

另外,请找到以下线程的链接供您参考:-

ASP.net WebForms中AzureAD和OpenIdConnect会话过期

使用Azure AD OpenIdConnect和自定义中间件的asp.net核心身份验证Cookie过期

感谢你,

最新更新