c# - C# - Azure SSO 令牌过期抛出错误
问题描述
我正在尝试编写使用 Azure 作为 SSO 提供程序的 ac# Web 应用程序。
我使用 Owin 作为中间层。
public void Configuration(IAppBuilder app)
{
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
// Sets the ClientId, authority, RedirectUri as obtained from web.config
ClientId = clientId,
Authority = authority,
RedirectUri = redirectUri,
// PostLogoutRedirectUri is the page that users will be redirected to after sign-out. In this case, it is using the home page
PostLogoutRedirectUri = redirectUri,
Scope = OpenIdConnectScope.OpenIdProfile,
// ResponseType is set to request the id_token - which contains basic information about the signed-in user
ResponseType = OpenIdConnectResponseType.IdToken,
// ValidateIssuer set to false to allow personal and work accounts from any organization to sign in to your application
// To only allow users from a single organizations, set ValidateIssuer to true and 'tenant' setting in web.config to the tenant name
// To allow users from only a list of specific organizations, set ValidateIssuer to true and use ValidIssuers parameter
TokenValidationParameters = new TokenValidationParameters()
{
ValidateIssuer = false // Simplification (see note below)
},
// OpenIdConnectAuthenticationNotifications configures OWIN to send notification of failed authentications to OnAuthenticationFailed method
Notifications = new OpenIdConnectAuthenticationNotifications
{
AuthenticationFailed = OnAuthenticationFailed
}
});
}
所以它可以正常登录,但是在我尝试执行 AJAX 请求1 小时后(无论页面是否已刷新),由于令牌已过期,我收到了 CORS 错误。
我如何“让它活着”令牌,以便用户没有 1 小时来完成他们的工作?
解决方案
令牌生命周期策略是一种包含令牌生命周期规则的策略对象。使用策略的属性来控制指定的令牌生命周期。如果未设置任何策略,系统将强制执行默认生命周期值。
您可以将访问令牌有效期设置为一天,这样您就不会因一小时的限制而过期。
您可以在服务主体、应用程序或租户上设置令牌生命周期配置。
您需要使用 Powershell 创建描述所需行为的策略,并将其链接到您的服务主体、租户或应用程序。请记住,如果您正在构建多租户应用程序,租户的所有者可以覆盖您的策略。
注意:不要依赖应用程序中的令牌生命周期,因为它可以随时更改。
您可以使用Azure AD Powershell 命令设置这些属性。然后运行以下命令来设置访问令牌生命周期:
1.登录Powershell。
Connect-AzureAD -Confirm
2.创建一个新策略,将 Access Token 生命周期设置为 2 小时。您可以将其更改为 10 分钟到 1 天之间。
New-AzureADPolicy -Definition @('{"TokenLifetimePolicy":{"Version":1,"AccessTokenLifetime":"24:00:00","MaxAgeSessionSingleFactor":"02:00:00"}}') -DisplayName "WebPolicyScenario" -IsOrganizationDefault $false -Type "TokenLifetimePolicy"
3.获取策略的ObjectId。
Get-AzureAdPolicy
4.将新策略链接到您的应用程序。您可以使用GraphExplorer获取应用程序的 objectId 。
Add-AzureADApplicationPolicy -Id <ObjectId of the Application> -RefObjectId <ObjectId of the Policy>
有关更多详细信息,您可以参考这篇关于Azure AD Configurable Token Lifetime的文章。
推荐阅读
- html - 将标题与第二列内容对齐
- c# - 如何在瞄准时做出预测轨迹弧?
- java - Java Bouncy Castle 生成的 ES256 密钥不适用于 JWT.io
- c++ - 未解析的外部符号 __imp__Py_Finalize
- javascript - 在 Node.JS 中向客户端发送通知
- java - AnyLogic 仿真用户界面屏幕中的工具提示/鼠标悬停文本?
- r - 使用 Shinymeta 在模块之间进行通信时出现缩进问题
- javascript - 标题 JavaScript 的新行
- java - KeeperErrorCode = 未使用文档版本实现
- c++ - 模板数组类无法正常工作