C#-Azure SSO令牌过期抛出错误
我正在尝试编写一个c#web应用程序,它使用Azure作为SSO提供商 我使用Owin作为中间层C#-Azure SSO令牌过期抛出错误,c#,azure,web-applications,single-sign-on,C#,Azure,Web Applications,Single Sign On,我正在尝试编写一个c#web应用程序,它使用Azure作为SSO提供商 我使用Owin作为中间层 public void Configuration(IAppBuilder app) { app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType); app.UseCookieAuthenticat
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
}
});
}
因此,它可以正常登录,但在1小时后当我尝试执行AJAX请求时(无论页面是否刷新),我会收到一个CORS错误,因为令牌已过期
如何使令牌保持活动状态,以便用户没有1小时的时间来完成工作?令牌生存期策略是一种包含令牌生存期规则的策略对象。使用策略的属性控制指定的令牌生存期如果未设置策略,系统将强制执行默认的生存期值。 您可以将访问令牌生存期设置为一天,这样您就不会超过一小时的限制 您可以在服务主体、应用程序或租户上设置令牌生存期配置 您需要使用Powershell创建一个描述所需行为的策略,并将其链接到您的服务主体、租户或应用程序。请记住,如果您正在构建多租户应用程序,租户的所有者可以覆盖您的策略 注意:不要依赖应用程序中的令牌生存期,因为它随时可能发生变化 可以使用设置这些属性。然后运行以下命令设置访问令牌生存期: 1.登录到Powershell
Connect-AzureAD -Confirm
2.创建新策略以将访问令牌生存期设置为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.将新策略链接到您的应用程序。您可以使用获取应用程序的objectId
Add AzureADApplicationPolicy-Id-reObjectId
有关更多详细信息,请参阅本文
Add-AzureADApplicationPolicy -Id <ObjectId of the Application> -RefObjectId <ObjectId of the Policy>