簡體   English   中英

OAuth 2.0 授權碼授權流程與客戶端憑證授權流程

[英]OAuth 2.0 auth code grant flow vs client credential grant flow

我們有一個 3rd 方移動應用程序。 在登錄過程中創建訪問令牌以使用授權代碼授予流程訪問我們的 API(.netcore)之一。

https://docs.microsoft.com/en-us/azure/active-directory/develop/v1-protocols-oauth-code

在此處輸入圖像描述

在此處輸入圖像描述

移動應用程序顯示許多圖塊。 登錄后,當用戶單擊其中一個圖塊時,我想調用另一個 .netcore API(使用 access_token)。

我計划將客戶端憑據流用於第二個 API 調用,因為它不需要用戶交互。

https://docs.microsoft.com/en-us/azure/active-directory/develop/v1-oauth2-client-creds-grant-flow

在此處輸入圖像描述

但是 API 端點(在代碼中)檢查聲明以獲取用戶 ID,並且客戶端憑據流創建一個沒有用戶信息的 jwt 令牌(因為沒有用戶交互)。

我使用正確的流程嗎? 單擊磁貼時是否可以使用授權代碼授予流程(無需用戶交互)?

您只能在使用需要用戶交互的身份驗證代碼流時獲取用戶信息。

我注意到您使用的是 v1.0 端點,您可以將 api uri 放在資源參數中。 v1.0 端點不需要 Scope 參數。 您可以在登錄后靜默獲取訪問令牌。

這是供您參考的代碼片段。

 // Because we signed-in already in the WebApp, the userObjectId is know
                string userObjectID = (User.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier"))?.Value;

                // Using ADAL.Net, get a bearer token to access the TodoListService
                AuthenticationContext authContext = new AuthenticationContext(AzureAdOptions.Settings.Authority, new NaiveSessionCache(userObjectID, HttpContext.Session));
                ClientCredential credential = new ClientCredential(AzureAdOptions.Settings.ClientId, AzureAdOptions.Settings.ClientSecret);
                result = await authContext.AcquireTokenSilentAsync(AzureAdOptions.Settings.TodoListResourceId, credential, new UserIdentifier(userObjectID, UserIdentifierType.UniqueId));

                // Retrieve the user's To Do List.
                HttpClient client = new HttpClient();
                HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, AzureAdOptions.Settings.TodoListBaseAddress + "/api/todolist");
                request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
                HttpResponseMessage response = await client.SendAsync(request);

參考:

活動目錄-dotnet-webapp-webapi-openidconnect-aspnetcore

暫無
暫無

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

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