ajax - 当会话 cookie 过期时,使用 AzureAD OIDC 协议的 asp.net mvc 应用程序的 Ajax 请求失败
问题描述
我有一个 asp.net mvc 5 应用程序,它使用 OpenID Connect (OIDC) 协议在 Azure Active Directory (Azure AD) 上提供身份验证。当用户使用 MVC Web App 启动一个新会话时,会返回一个新的 cookie 来控制这个会话。这个“会话 cookie”是基于“ID Token”创建的,只要这个 cookie 有效,用户就会被认为是 MVC Web App 中的认证用户。这里默认情况下,“id_token”会持续一小时,这个持续时间对于“session cookie”来说是一样的
我按照以下链接进行实施
https://jamuro-blog.azurewebsites.net/post/managing-azure-ad-sessions-and-ajax-calls-in-aspnet-mvc-apps-with-open-id-connect-authentication http://www .cloudidentity.com/blog/2016/07/25/controlling-a-web-apps-session-duration-2/
这是我的代码:
启动.cs
public partial class Startup
{
public void ConfigureAuth(IAppBuilder app)
{
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseKentorOwinCookieSaver();
app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
CookieSecure = CookieSecureOption.Always
});
app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions()
{
ClientId = clientId,
Authority = authority,
TokenValidationParameters = new TokenValidationParameters()
{
ValidateAudience = true,
ValidateIssuer = true
},
Notifications = new OpenIdConnectAuthenticationNotifications()
{
AuthorizationCodeReceived = new Func<AuthorizationCodeReceivedNotification, Task>(this.OnAuthorizationCodeReceived),
AuthenticationFailed = new Func<AuthenticationFailedNotification<OpenIdConnectMessage, OpenIdConnectAuthenticationOptions>, Task>(this.OnAuthenticationFailed),
RedirectToIdentityProvider = (RedirectToIdentityProviderNotification<OpenIdConnectMessage, OpenIdConnectAuthenticationOptions> context) => {
string appBaseUrl = string.Concat(new object[] { context.Request.Scheme, "://", context.Request.Host, context.Request.PathBase });
context.ProtocolMessage.RedirectUri = appBaseUrl;
context.ProtocolMessage.DomainHint = tenant;
return Task.FromResult(0);
}
},
UseTokenLifetime = true
});
}
}
这里根据我的分析 SlidingExpiration 值为 true
app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
CookieSecure = CookieSecureOption.Always
});
我还使用文章中提到的 iFrame 实现了更新会话的逻辑。
这篇文章和我的实现之间的唯一区别是我没有明确设置 SlidingExpiration = false
这是我的测试场景,以防“会话 cookie”过期
- 如果下一次调用 MVC Web 应用程序时未使用 AJAX,则后端包含的 ADAL MW 将检测到此问题并尝试自动更新 cookie。执行此操作时,您可以看到 Web 应用程序连接到 Azure,然后或多或少地以适当的方式返回到初始应用程序调用页面。
- 如果下一次调用 MVC Web 应用程序是使用 AJAX 进行的,那么我会看到一个问题。
总之,我想说如果会话 cookie 已过期,然后我在不使用 AJAX 的情况下执行任何操作,它就可以工作,但是如果我执行任何操作然后涉及 AJAX 的使用,那么它就会中断。
这是我用于测试场景的时间:
10:00 AM -> 我登录应用程序并登陆 Screen_1
10:00 AM – 10:20 AM -> 我留在 Screen_1
上午 10:20 -> 我导航到 Screen_2
10:20 AM – 10:40 AM -> 我留在 Screen_2
上午 10:40 -> 我导航到 Screen_3
10:40 AM – 11:05 -> 我留在 Screen_3
上午 11:06 -> 我单击了一个涉及 AJAX 操作的按钮,我看到了错误。
谁能帮我解决这个问题?
解决方案
推荐阅读
- node.js - 尝试通过 Typeorm 将值插入 Mssql 数据库时出现“ERR_INVALID_ARG_TYPE”错误
- ksh - 如何在变量 $File Equals 中使用的文件名中包含变量
- sql - 添加 order by 时查询退化为 PK 扫描
- .net-core - 在 .net core 3.1 中生成模式
- c# - 如何将 SharePoint 2013 列表中的数据导出到可以打印为 PDF 的空白页中
- web-config - 我应该如何编写 web.config 规则以免它们发生冲突?
- java - 在 android studio 中构建一个 maven 项目
- java - Groovy 中用于更新和插入 Initial_Range 和 Final_Range 的脚本
- python - 如何使用 Python (SciPy) 解决一定数量的时间步长的 ODE?
- javascript - Promise 被解析为 undefined