gpt4 book ai didi

msal - 使用 MSAL 访问 token /刷新 token

转载 作者:行者123 更新时间:2023-12-01 10:21:30 32 4
gpt4 key购买 nike

我对 OAuth2 以及 AccessToken 和 RefreshToken 的概念比较熟悉。

看起来 MSAL 在使用 ClientApplicationBase.AcquireTokenSilentAsync() 时为我们做了一些工作。

我不清楚它是否会始终检查当前 AccessToken 的过期时间并在方法调用时自动刷新它(使用 RefreshToken)。

无论如何,对于我们应该多久调用一次 AcquireTokenSilentAsync() 是否有“最佳实践”?我们是否应该自己跟踪到期时间并调用此方法来更新我们的承载身份验证 header ?我们应该在每个请求上调用 AcquireTokenSilentAsync() 吗? (疑)

我看不到使用 DelegateAuthenticationProvider 的 GraphServiceClient(我知道的切线主题)将如何做任何有用的 WRT 刷新。当 token 即将到期时,我们是否需要扩展该类并执行我们自己的刷新?我觉得这会/应该已经在 SDK 中了。

感谢您提供任何提示。
-AJ

最佳答案

2020 年 11 月更新
此答案最初是为 MSAL 客户端的 V2 编写的。此后发布了 V3,其工作方式可能与 V2 不同。
原始答案

I'm not clear as to whether it will always check the expiration of the current AccessToken and automatically refresh it (using the RefreshToken) on method call.


如果我正确理解 this answer,则在提供 offline_access 范围时会自动提供刷新 token

...you've requested the offline_access scope so your app receives a Refresh Token.

AcquireTokenSilentAsyncdescription 意味着当提供刷新 token 时,它将检查 token 上的过期日期,如果它已过期或接近过期,则获取一个新 token 。

If access token is expired or close to expiration (within 5 minutewindow), then refresh token (if available) is used to acquire a newaccess token by making a network call.


它将重复此行为,直到刷新 token 过期。或者,您可以通过 utilizing forceRefresh 上的 AcquireTokenSilentAsync 参数通过刷新 token 强制刷新访问 token
enter image description here
最后,我将引用 this answer on SO 因为它提供了关于 MSAL 和 token 的很好的见解

Just to make a small clarification, MSAL doesn't actually issue tokensor decide a token expiration, but rather ingests an acquires tokenfrom the Azure AD STS.

MSAL will automatically refresh your access token after expirationwhen calling AcquireTokenSilentAsync. .... The default tokenexpirations right now are:

Access Tokens: 1 hour

Refresh Tokens: 90 days, 14 day inactive sliding window


(2017 年 6 月 13 日)

Regardless, is there a "best practice" for how often we should callAcquireTokenSilentAsync() ? Should we keep track of the expirationourselves and call this method to update our bearer authenticationheader? Should we be calling AcquireTokenSilentAsync() on everyRequest?


documentation 还列出了用于调用 AcquireTokenSilentAsync 的“推荐调用模式”。文档 also 提到

For both Public client and confidential client applications, MSAL.NET maintains a token cache (or two caches in the case of confidential client applications), and applications should try to get a token from the cache first before any other means.


根据我看到的示例,包括文档中推荐的调用模式,我认为您可以简单地调用 AcquireTokenSilentAsync 并捕获 MsalUiRequiredException 作为 token 已过期且用户必须再次登录的指示。

I can't see how the GraphServiceClient (tangent topic, I know) using the DelegateAuthenticationProvider will do anything helpful WRT refreshing. Do we need to extend that class and perform our own refresh when the token is nearing expiration? I feel like this would/should be already in the SDK.


如果我正确理解了 DelegateAuthenticationProvider,它所做的就是在我们将它传递给 Graph 之前修改 requestMessage。我们要做的就是为我们的访问 token 提供请求的授权 header 。我们已经知道,当我们获取访问 token 时,它是有效的,因此我们可以添加它。
        new DelegateAuthenticationProvider(async (requestMessage) =>
{
ConfidentialClientApplication cca = new ConfidentialClientApplication(_ClientId, _Authority, _RedirectUri, new ClientCredential(_ClientSecret), _UserTokenSessionCache.GetTokenCache(identifier, httpContext), _ApplicationTokenCache.GetTokenCache());
AuthenticationResult result = await cca.AcquireTokenSilentAsync();
requestMessage.Headers.Add("Authorization", result.CreateAuthorizationHeader());
//OR
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", result.AccessToken);
});
(在任何一种设置标题的方式之间都有 no difference )
我一直在这条路上,这对我有用。我强烈建议阅读他们的文档,因为它确实对如何实现 MSAL.Net 提供了很好的见解。
我还没有时间来处理 token 持续时间。如果没有提供刷新 token ,则行为也不行(如果可能的话)
我希望这有帮助!

关于msal - 使用 MSAL 访问 token /刷新 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51332122/

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