久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Azure - AD - AcquireTokenSilent 給出錯誤 failed_to_acquir

Azure - AD - AcquireTokenSilent giving error failed_to_acquire_token_silently(Azure - AD - AcquireTokenSilent 給出錯誤 failed_to_acquire_token_silently)
本文介紹了Azure - AD - AcquireTokenSilent 給出錯誤 failed_to_acquire_token_silently的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我們使用 Azure AD 進行身份驗證并每 30 分鐘獲取一次刷新的訪問令牌.我們調用下面的方法來獲取安全令牌并將其添加到請求頭中.

We are using Azure AD to authenticate and get the refreshed access token every 30 mins. We invoke below method which acquires security token and add it to request header.

var userObjectId = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;
var authContext = new AuthenticationContext(Authority, new NaiveSessionCache(userObjectId));
var credential = new ClientCredential(ConfigurationManager.AppSettings["ida:ClientId"],
ConfigurationManager.AppSettings["ida:ClientSecret"]);

    try
    {
    var authenticationResult = authContext.AcquireTokenSilent(ConfigurationManager.AppSettings["WebAPIBaseAddress"], credential, new UserIdentifier(userObjectId, UserIdentifierType.UniqueId));
    //set cookie for azure oauth refresh token - on successful login
    var httpCookie = HttpContext.Current.Response.Cookies["RefreshToken"];
    if (httpCookie != null)
        httpCookie.Value = authenticationResult.RefreshToken;

    request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", authenticationResult.AccessToken);
    }
    catch
    {
    //Get access token using Refresh Token 
    var authenticationResult = authContext.AcquireTokenByRefreshToken(httpCookie.Value, credential, ConfigurationManager.AppSettings["WebAPIBaseAddress"]);
    }

在上述方法中,我們使用了 AcquireTokenSilent 方法,它為我們提供了訪問令牌.由于訪問令牌僅持續一段時間.過期后,我們調用 AcquireTokenByRefreshToken 獲取刷新令牌.

In above method, we have used AcquireTokenSilent method which gives us access token. Since access token lasts only for certain period of time. After its expiry, we call AcquireTokenByRefreshToken to get refresh token.

上面的代碼運行良好,但是我們隨機出現以下異常:

The above code works well, however we are getting below exception randomly:

Microsoft.IdentityModel.Clients.ActiveDirectory.AdalSilentTokenAcquisitionException: Failed to acquire token silently. Call method AcquireToken 
   at Microsoft.IdentityModel.Clients.ActiveDirectory.AcquireTokenSilentHandler.SendTokenRequestAsync() 
   at Microsoft.IdentityModel.Clients.ActiveDirectory.AcquireTokenHandlerBase.<RunAsync>d__0.MoveNext()
ErrorCode: failed_to_acquire_token_silently

這種不一致的行為可能是什么原因?相同的代碼在少數環境(Stage/Dev)上工作,但它在生產環境中隨機拋出錯誤.

What could be the reason of such inconsistent behaviour? The same code is working on few environments (Stage/Dev) but its throwing error randomly on Production.

請提出建議.

推薦答案

我們能夠解決這個問題.這似乎是代碼本身的一個小錯誤.當 AccessToken 過期時,它會拋出一個異常,并嘗試在 catch 塊中使用 AcquireTokenByRefreshToken 獲取一個新的.這里我們沒有在 Cookie 中設置新收到的刷新令牌.我們還需要在 catch 塊中添加以下語句,以便它可以獲取 Refresh 令牌,然后可以將其傳回以生成新的 Access Token.

We were able to resolve this. It seems to be a small mistake in the code itself. When the AccessToken expires, it throws an exception and it tries to fetch a new one using AcquireTokenByRefreshToken in the catch block. Here we were not setting the newly received refresh token back in the Cookie. We need to add below statement in the catch block also, so that it would get the Refresh token, which can then be passed back to generate a new Access Token.

httpCookie.Value = authenticationResult.RefreshToken;

這篇關于Azure - AD - AcquireTokenSilent 給出錯誤 failed_to_acquire_token_silently的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

ASP.NET Core authenticating with Azure Active Directory and persisting custom Claims across requests(ASP.NET Core 使用 Azure Active Directory 進行身份驗證并跨請求保留自定義聲明)
ASP.NET Core 2.0 Web API Azure Ad v2 Token Authorization not working(ASP.NET Core 2.0 Web API Azure Ad v2 令牌授權不起作用)
ASP Core Azure Active Directory Login use roles(ASP Core Azure Active Directory 登錄使用角色)
How do I get Azure AD OAuth2 Access Token and Refresh token for Daemon or Server to C# ASP.NET Web API(如何獲取守護進程或服務器到 C# ASP.NET Web API 的 Azure AD OAuth2 訪問令牌和刷新令牌) - IT屋-程序員軟件開發技
.Net Core 2.0 - Get AAD access token to use with Microsoft Graph(.Net Core 2.0 - 獲取 AAD 訪問令牌以與 Microsoft Graph 一起使用)
Azure KeyVault Active Directory AcquireTokenAsync timeout when called asynchronously(異步調用時 Azure KeyVault Active Directory AcquireTokenAsync 超時)
主站蜘蛛池模板: 国产精品一区二区视频 | 亚洲免费福利视频 | 成人三级视频 | 美女一级毛片 | www.婷婷| 中文字幕视频在线看 | 色综合99 | 在线观看免费av网 | 亚洲精品av在线 | 日韩另类 | 91久久久久久久久久久久久 | www视频在线观看 | 亚洲高清在线视频 | 欧美a在线 | 国产精品日日摸夜夜添夜夜av | 在线成人福利 | 亚洲一区二区三区免费 | 国产精品日日摸夜夜添夜夜av | 午夜免费看视频 | 亚洲欧美综合 | 亚洲国产精品99久久久久久久久 | 久久国产精品无码网站 | 97日韩精品 | 三级av在线 | 中文字幕在线免费 | 欧美福利 | 99综合| 亚洲 中文 欧美 日韩 在线观看 | 97国产精品 | 国产一区二区在线免费观看 | 亚洲一区二区三区免费视频 | av片在线播放 | 欧美日韩三区 | 国产精品久久久久久婷婷天堂 | 午夜小视频免费观看 | 亚洲精品国产a久久久久久 午夜影院网站 | 国产亚洲精品久久久优势 | 色婷婷婷婷色 | 成人亚洲 | 国产精品久久久久久久久免费 | 欧美黄色小视频 |