gpt4 book ai didi

asp.net-mvc - 使用 Azure AD 和 ADAL 3 的 MVC 应用程序 - 身份验证 Cookie 在 1 小时后过期

转载 作者:行者123 更新时间:2023-12-03 03:08:52 25 4
gpt4 key购买 nike

我使用 Azure AD 和 OAuth 2 以及 Open ID Connect 来开发 MVC Web 应用程序以进行用户授权。当 token 在 60 分钟后过期时(这很好),每个文档 token 都会自动刷新。现在的问题是,要获取 token ,我需要知道存储在 cookie 中的当前经过身份验证的用户。获取Token的代码是这样的:

public async Task<AuthenticationToken> GetTokenForApplication(string resourceID)
{
string signedInUserID = ClaimsPrincipal.Current.SignedinUserId();
var tenantID = ClaimsPrincipal.Current.TenantId();
string userObjectID = ClaimsPrincipal.Current.SignedinUserObjectId();

// get a token for the Graph without triggering any user interaction (from the cache, via multi-resource refresh token, etc)
ClientCredential clientcred = new ClientCredential(Config.ClientId, Config.AppKey);
// initialize AuthenticationContext with the token cache of the currently signed in user, as kept in the app's database
AuthenticationContext authenticationContext = new AuthenticationContext(string.Format("{0}{1}", Config.AadInstance, tenantID), new ADALTokenCache(signedInUserID));
AuthenticationResult authenticationResult = await authenticationContext.AcquireTokenSilentAsync(resourceID, clientcred, new UserIdentifier(userObjectID, UserIdentifierType.UniqueId));
var token = new AuthenticationToken(authenticationResult.AccessToken) { ExpiresOn = authenticationResult.ExpiresOn };

return token;
}

现在我陷入了困境,ClaimsPrincipal.Current.SignedinUserId() 方法调用抛出空引用异常。当我检查 ClaimsPrincipal.Current 对象时,没有有关登录用户的数据可用。但这是更新/请求 token 所需的信息。

MVC Web 应用程序的最佳实践是什么?有没有办法延长 cookie 的有效性,或者有没有办法重新验证当前用户而不重定向到 Web 应用程序的根页面?

经过更多研究后,我发现这两页描述了一些很好的解决我的问题的选项: Controlling a Web App’s session durationASP.NET-Identity-Cookie-Authentication-Timeouts

这些是好的方法吗?

最佳答案

经过更多研究后,我发现这两页描述了一些很好的解决我的问题的选项: Controlling a Web App’s session durationASP.NET-Identity-Cookie-Authentication-Timeouts

这些是好的方法吗?

关于asp.net-mvc - 使用 Azure AD 和 ADAL 3 的 MVC 应用程序 - 身份验证 Cookie 在 1 小时后过期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41526417/

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