久久久久久久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 超時)
主站蜘蛛池模板: 91精品国产综合久久久久久丝袜 | 精品久久久久久久久久久 | 欧美日韩久久 | 日韩视频在线观看中文字幕 | av色站| 国产欧美精品一区二区色综合朱莉 | 国产精品a一区二区三区网址 | 亚洲国产精品一区在线观看 | 欧美一二三区 | 亚洲精彩视频在线观看 | 中文日韩在线视频 | 北条麻妃视频在线观看 | 国产精品亚洲综合 | 一级aaaaaa毛片免费同男同女 | 国产成人精品一区二区三区视频 | 国产乱码精品一品二品 | 丝袜久久 | aaa精品| 午夜久久久 | 亚洲第一色站 | 午夜免费在线观看 | 激情福利视频 | 免费看a | 中文字幕日韩一区 | 草草草网站 | 国产精品免费一区二区三区四区 | 91精品国产91久久久久久吃药 | 色网站在线| 欧美一区二区网站 | 久久av网 | 欧美日韩在线观看一区 | 久久伊人影院 | 日韩欧美在线一区二区 | 成人福利视频 | 日本一区二区视频 | 久久久免费少妇高潮毛片 | 欧美日韩国产在线观看 | 九九综合九九 | 国产欧美视频一区二区 | 波多野结衣亚洲 | 日本成人午夜影院 |