gpt4 book ai didi

c# - 由于 Azure 广告 OpenIdConnectAuthentication 中的访问 token , session 将在 1 小时后过期

转载 作者:太空宇宙 更新时间:2023-11-03 12:37:45 24 4
gpt4 key购买 nike

在我们的应用程序中,我们使用 Azure Ad OpenIdConnectAuthentication 进行登录,在调用我们的应用程序时将重定向到“https://login.microsoftonline.com/

我认为由于某种原因刷新 token 没有在我们的单页应用程序中生成,并由于访问 token (将在 1 小时后过期)而迫使用户在 1 小时后注销。

我读了太多博客,但我无法得到答案。非常感谢任何建议。

这是我的代码:

app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
ClientId = clientId,
Authority = Authority,
PostLogoutRedirectUri = postLogoutRedirectUri,

Notifications = new OpenIdConnectAuthenticationNotifications()
{

AuthorizationCodeReceived = (context) =>
{
var code = context.Code;

ClientCredential credential = new ClientCredential(clientId, appKey);
string userObjectID = context.AuthenticationTicket.Identity.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;
AuthenticationContext authContext = new AuthenticationContext(Authority, new CustomTokenCache(userObjectID));

//getting the tokens from below line (Access token with expiry time)
AuthenticationResult result = authContext.AcquireTokenByAuthorizationCode(code, new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path)), credential, graphResourceId);
return Task.FromResult(0);
},

AuthenticationFailed = context =>
{
context.HandleResponse();
context.Response.Redirect("/");
return Task.FromResult(0);
}

}

最佳答案

对于 SPA 应用,我们通常通过隐式流程而不是授权代码授予流程获取 token 。

token 将直接从授权端点返回,而不是从 token 端点返回。我们可以通过将应用的 list oauth2AllowImplicitFlow 属性修改为 true 来启用隐式流。

要在隐式流程中过期时更新访问 token ,我们可以执行隐藏的 iframe 请求并添加 prompt 参数并将其值设置为 none以便用户无需再次输入其凭据。

为了方便开发,我们可以使用微软为SPA应用程序提供的ADAL库。我们可以通过AuthenticationContext.prototype._renewToken方法更新 token 。

有关隐式流程的更多详细信息,您可以引用以下链接:

Authentication Scenarios for Azure AD -(单页应用程序(SPA)部分)

Understanding the OAuth2 implicit grant flow in Azure Active Directory (AD)

v2.0 Protocols - SPAs using the implicit flow

关于c# - 由于 Azure 广告 OpenIdConnectAuthentication 中的访问 token , session 将在 1 小时后过期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40256743/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com