首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OAuth 2.0认证码授权流程与客户端凭据授权流程

OAuth 2.0认证码授权流程与客户端凭据授权流程
EN

Stack Overflow用户
提问于 2019-11-05 17:12:04
回答 1查看 418关注 0票数 1

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

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

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

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

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

但是API端点(在代码中)检查声明以获取userID,并且客户端凭据流会创建一个JWT令牌,而不包含用户信息(因为没有用户交互)。

我使用的流程是正确的吗?有没有办法在点击磁贴时使用授权码授权流程(不需要用户交互)?

EN

回答 1

Stack Overflow用户

发布于 2019-11-05 17:35:13

只有在使用需要用户交互的认证码流时,才能获取用户信息。

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

下面是供您参考的代码片段。

代码语言:javascript
复制
 // 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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58707927

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档