Lastly, I am going to quote this answer on SO since it gives a nice insight about MSAL and tokens到期后,MSAL将自动刷新您的访问令牌 调用AcquireTokenSilentAsync时. ....默认令牌 现在到期的是:MSAL will automatically refresh your access token after expiration when calling AcquireTokenSilentAsync. .... The default token expirations right now are:访问令牌:1小时刷新令牌:90天,14天无效的滑动窗口Refresh Tokens: 90 days, 14 day inactive sliding window(17年6月13日) 文档还列出了用于调用AcquireTokenSilentAsync的推荐的呼叫模式".文档也提到The documentation also lists a 'Recommended call pattern' for calling the AcquireTokenSilentAsync. The documentation also mentions that基于我所看到的示例,包括文档中推荐的调用模式,我认为您可以简单地调用AcquireTokenSilentAsync并捕获MsalUiRequiredException,以表明令牌已过期并且用户必须登录再次进入.Based on examples I've seen, including the recommended call pattern from the documentation, I would argue you could simply call AcquireTokenSilentAsyncand catch the MsalUiRequiredException as an indication that the token has expired and the user has to log in again.如果我正确理解了DelegateAuthenticationProvider,它所做的就是在将requestMessage传递给Graph之前对其进行修改.我们要做的就是为访问令牌提供请求的授权标头.我们已经知道,当我们获取访问令牌时,它是有效的,因此我们只需添加它即可.If I understand the DelegateAuthenticationProvider correctly, what it does is modify the requestMessage before we pass it to Graph. All we got to do is provide our access token with an authorization header for the request. We already know that when we fetch our access token, it is valid, so we can just add it. 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); });(有设置标头的两种方法之间没有区别)我一直走这条路,这对我有用.我强烈建议您阅读他们的文档,因为它确实对如何实现MSAL.Net提供了很好的见识.I've been down this path and this does the trick for me. I highly advise reading their documentation, because it does gives a good insight in how to implement MSAL.Net.我还没有时间来玩令牌的期限.如果没有提供刷新令牌(即使有可能的话)也没有这种行为I haven't had time yet to play around with the token durations yet. Nor the behavior if no refresh token is provided (if that's even possible)我希望这会有所帮助! 这篇关于使用MSAL访问令牌/刷新令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!