簡體   English   中英

ADB2C - 在令牌緩存過期時處理“在 AcquiretokenSilent API 中傳遞了空用戶”錯誤

[英]ADB2C - Handling the "Null user was passed in AcquiretokenSilent API" error when token cache expires

我已經設法將我的應用程序配置為使用 ADB2C 進行身份驗證,並且它似乎工作正常。 實現的 ADB2C 代碼是對 Microsoft 示例之一的調整,其中他們使用 SessionTokenCache 類來管理 TokenCache 的實例。 在我的應用程序中,我按如下方式檢索訪問令牌:

private async Task<string> _getAccessToken(IConfidentialClientCredentials credentials)
        {
            if (this.HasCredentials())
            {
                var clientCredential = new ClientCredential(credentials.ClientSecret);
                var userId = this._getUserIdClaimValue();

                var tokenCache = new SessionTokenCache(_httpContextResolver.Context, userId);                

                var confidentialClientApplication = new ConfidentialClientApplication(
                    credentials.ClientId,
                    credentials.Authority,
                    credentials.RedirectUri,
                    clientCredential,
                    tokenCache.GetInstance(),
                    null);

                IAccount account = confidentialClientApplication.GetAccountsAsync().Result.FirstOrDefault();
                if (account == null)
                {
                    return "";
                }

                var authenticationResult = await confidentialClientApplication.AcquireTokenSilentAsync(
                    credentials.ApiScopes.Split(' '),
                    account,
                    credentials.Authority,
                    false);

                return authenticationResult.AccessToken;
            }
            else
            {
                return "";
            }
        }

此方法用於獲取訪問令牌並將其傳遞到 HttpClient 的請求頭中,如下所示:

...

 using (var request = new HttpRequestMessage(HttpMethod.Get, address.AbsoluteUri))
            {
                if (this.HasCredentials())
                {
                    string accessToken = await this._getAccessToken(_confidentialClientCredentials);

                    request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
                }

                using (HttpResponseMessage response = await this.SendAsync(request))
                {
                    //result-processing logic
                }
...

問題在於,當應用程序重新啟動時,用戶仍然通過 ADB2C cookie 進行身份驗證,但confidentialClientApplication.GetAccountsAsync().Result.FirstOrDefault(); 返回空值。 這可能是因為令牌緩存在應用程序重啟時被破壞,所以我可能可以使用 Redis 緩存來修復。

然而,我的主要問題是如何處理擁有空帳戶但同時被“驗證”的情況。 即使我的帳戶為空,我對網站的請求如何得到驗證? 例如,它不應該失敗並將我重定向到登錄頁面嗎?

我嘗試查看授權過濾器,並使用以下代碼連接到身份驗證過程並驗證用戶是否為空,但無濟於事。 永遠不會調用以下事件(這是在 ConfigureServices 中):

services.AddAuthentication(AzureADB2CDefaults.AuthenticationScheme)            
            .AddAzureADB2C(options => Configuration.Bind("ActiveDirectoryB2C", options))
            .AddAzureADB2CBearer(options => Configuration.Bind("ActiveDirectoryB2C", options))
            .AddCookie((options) => new CookieAuthenticationOptions
            {
                Events = new CookieAuthenticationEvents
                {
                    OnValidatePrincipal = context =>
                    {
                        // context.Principal gives you access to the logged-in user
                        // context.Properties.GetTokens() gives you access to all the tokens

                        return Task.CompletedTask;
                    },
                    OnSignedIn = context =>
                    {
                        return Task.CompletedTask;
                    }
                }
            });

對我來說,這一切都太抽象了,無法理解正在發生的事情。 要么如此,要么我錯過了一些基本的東西。

注意:如果我嘗試將空帳戶傳遞給 secretClientApplication.AcquireTokenSilentAsync confidentialClientApplication.AcquireTokenSilentAsync()方法,則會拋出錯誤“空用戶已在 AcquiretokenSilent API 中傳遞。傳入用戶對象或調用acquireToken 驗證。 ”。

我用這個代碼解決了:

protected override void OnException(ExceptionContext filterContext)
{
 if (filterContext.Exception is Microsoft.Identity.Client.MsalUiRequiredException)
 {
   RedirectToAction("SignIn", "Account");
 }
 else {
    //Do your logging
    // ...
 }
}

我會尋找更好的解決方案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM