c# - Azure AD 身份验证在会话超时时中断 HTTP 发布操作
问题描述
我最近使用“开箱即用”代码从 Windows 身份验证更改为 Azure AD;
public void ConfigureAuth(IAppBuilder app)
{
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseKentorOwinCookieSaver();
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
ClientId = clientId,
Authority = Authority,
PostLogoutRedirectUri = postLogoutRedirectUri,
Notifications = new OpenIdConnectAuthenticationNotifications()
{
// If there is a code in the OpenID Connect response, redeem it for an access token and refresh token, and store those away.
AuthorizationCodeReceived = (context) =>
{
var code = context.Code;
ClientCredential credential = new ClientCredential(clientId, appKey);
string signedInUserID = context.AuthenticationTicket.Identity.FindFirst(ClaimTypes.NameIdentifier).Value;
//AuthenticationContext authContext = new AuthenticationContext(Authority, new ADALTokenCache(signedInUserID));
AuthenticationContext authContext = new AuthenticationContext(Authority);
return authContext.AcquireTokenByAuthorizationCodeAsync(
code, new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path)), credential, graphResourceId);
}
}
});
}
我们的用户在尝试提交某些表单时开始出现间歇性 404 错误。我想我已经设法通过删除 cookie 来重现这个问题,所以我怀疑它与会话自然超时的时间有关。
如果我查看带有 HTTP GET 请求的流程,它看起来像;
- HTTP GET https://myappurl/page?param1=value¶m2=value
- 重定向到https://login.microsoftonline.com的 HTTP 302 响应(包括各种参数;状态、client_id 等)
- HTTP 200 响应(不太确定它如何/为什么知道重定向)
- HTTP GET https://myappurl/
- HTTP 302 响应,重定向到原始 URL https://myappurl/page?param1=value¶m2=value
- HTTP GET https://myappurl/page?param1=value¶m2=value
- HTTP 200 响应
一切都是一种享受...
但是对于 HTTP POST;
- HTTP POST 到https://myappurl/another_page
- 重定向到https://login.microsoftonline.com的 HTTP 302 响应(包括各种参数;状态、client_id 等)
- HTTP 200 响应(不太确定它如何/为什么知道重定向)
- HTTP GET https://myappurl/
- 重定向到原始 URL https://myappurl/another_page的 HTTP 302 响应
- HTTP GET https://myappurl/another_page
- HTTP 404 响应
失败,因为端点只接受 HTTP POST 请求。
知道是否/如何解决这个问题吗?我会认为内置的状态跟踪或它正在做的任何事情都会存储原始请求并继续它离开的地方,不管......
解决方案
看起来您没有使用令牌缓存。这意味着用户的会话将在他们登录应用程序后大约一个小时后过期。
要解决此问题,您应该在应用程序需要访问令牌时使用 AcquireTokenSilentAsync。此方法将使用它的内存缓存自动为您刷新令牌。有关更多详细信息,请参阅https://github.com/AzureAD/azure-activedirectory-library-for-dotnet/wiki/AcquireTokenSilentAsync-using-a-cached-token
推荐阅读
- r - R:从 parse(text = ) 将参数匹配到表达式树
- css - Google Chrome Devtools - 搜索 CSS 选择器时不显示第一个找到的结果
- reactjs - 通过 nginx 托管的 React 应用程序在移动设备上停止响应
- java - 在 if else 语句中使用多个 charAt 的正确方法是什么?
- .htaccess - 多次调用配置网站不起作用
- node.js - Mikro-orm:如何将查询结果与聚合到自己的实体中?
- python - 从自身中减去平均数组值
- javascript - ngFor :在 ng 中使用 2 个索引以错误的方式更改值
- date - 日期时间功能
- java - Java:如何一遍又一遍地轮询一个小文件以查看它是否已更改?(观看无法工作)