OAuth 2.0身份验证代码授予流程与客户端证书授予流程

我们有一个第三方移动应用程序。哪个会在登录过程中创建一个访问令牌,以使用授权代码授予流程访问我们的API(.netcore)之一。

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

OAuth 2.0身份验证代码授予流程与客户端证书授予流程

OAuth 2.0身份验证代码授予流程与客户端证书授予流程

移动应用程序显示许多图块。登录后,当用户单击其中一个图块时,我想调用另一个.netcore API(使用access_token)。

我正计划在第二次API调用中使用客户端凭据流,因为它不需要用户交互。

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

OAuth 2.0身份验证代码授予流程与客户端证书授予流程

但是API端点(在代码中)检查Claims以获取用户ID,并且客户端凭证流会创建一个没有用户信息的jwt令牌(因为没有用户交互)。

我使用的流量正确吗?点击磁贴时,是否有一种使用授权码授予流程的方式(不需要用户交互)?

pangzi08 回答:OAuth 2.0身份验证代码授予流程与客户端证书授予流程

仅当使用需要用户交互的身份验证代码流时,您才能获取用户信息。

我注意到您正在使用v1.0端点,您可以将api uri放在resource参数中。 v1.0端点不需要范围参数。登录后,您可以静默获取访问令牌。

这是代码段供您参考。

 // 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);

参考:

active-directory-dotnet-webapp-webapi-openidconnect-aspnetcore

本文链接:https://www.f2er.com/3160951.html

大家都在问