我们有一个第三方移动应用程序。它在登录过程中创建访问令牌,以使用授权码授予流访问我们的一个应用程序接口(.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令牌,而不包含用户信息(因为没有用户交互)。
我使用的流程是正确的吗?有没有办法在点击磁贴时使用授权码授权流程(不需要用户交互)?
发布于 2019-11-05 17:35:13
只有在使用需要用户交互的认证码流时,才能获取用户信息。
我注意到您使用的是v1.0端点,您可以将api uri放在resource参数中。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);参考资料:
active-directory-dotnet-webapp-webapi-openidconnect-aspnetcore
https://stackoverflow.com/questions/58707927
复制相似问题