.net-core - OAuth 2.0 身份验证代码授权流程与客户端凭据授权流程
问题描述
我们有一个 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 端点不需要范围参数。您可以在登录后静默获取访问令牌。
这是供您参考的代码片段。
// 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);
参考:
推荐阅读
- outlook-addin - Outlook AddIn Mobile 问题 - 无法在移动设备中运行,而相同的代码适用于桌面和 Web 客户端
- javascript - 无法访问传递给组件 React 的对象
- postgresql - Postgresql:错误:“DO”处或附近的语法错误
- sql - SQL 中的逐行处理
- python - 使用python上传wav文件并保存在目录中
- c - 原始 TCP 套接字在发送初始数据后接收常量 PSH、ACK 数据包
- excel - 如何以某种格式在excel中打印今天的日期
- regex - Ansible lineinfile - apache 虚拟主机块
- excel - 从文件夹中最新的 excel 文件导入工作表?第一次使用VBA
- python - OpenCV2将图像转换为灰度并找到边缘但出现此错误