Azure AD 不同数量的客户端和服务器声明

Azure AD different number of client and server claims

我们需要将 AzureAD 身份验证流程集成到我们现有的项目中。所以我在 here 找到了示例代码。它基本上包含两个 Web 项目。一个 MVC 项目作为客户端,Web API 作为服务器。

我们虽然使用来自 JWT 令牌的 "upn" 声明来唯一标识登录用户。我 运行 示例代码并能够在 Azure AD 中对用户进行身份验证。当我检查我在 jwt.io 从 AAD 获得的访问令牌时,声明部分不包含 "upn" 但我能够使用 ClaimsPrincipal.Current.FindFirst(ClaimTypes.Upn) 在客户端上检索 "upn" .添加此访问令牌作为不记名授权 header 并调用网络 api。在这里,我无法使用 ClaimsPrincipal.Current.FindFirst(ClaimTypes.Upn) 检索 "upn" 声明。此声明的值为空。我需要做任何额外的配置才能进入服务器端吗?截至目前,我只能在客户端检索 upn。

相关示例项目:

calls a web API under the application's identity, instead of the user's identity

在 TodoListController 的第 95 行,它获取令牌:

result = await authContext.AcquireTokenAsync(todoListResourceId, clientCredential);

它正在调用 API 作为自身,因此它是一个 app-only 调用,而不是委托调用。因此没有 UPN。你会发现一个 appid 声明,虽然它标识了调用应用程序。

查看另一个对 API 进行委托调用的示例:https://github.com/Azure-Samples/active-directory-dotnet-webapp-webapi-openidconnect/blob/master/TodoListWebApp/Controllers/TodoListController.cs

如您所见,获取令牌的调用完全不同:

string userObjectID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;
AuthenticationContext authContext = new AuthenticationContext(Startup.Authority, new NaiveSessionCache(userObjectID));
ClientCredential credential = new ClientCredential(clientId, appKey);
result = await authContext.AcquireTokenSilentAsync(todoListResourceId, credential, new UserIdentifier(userObjectID, UserIdentifierType.UniqueId));

注意认证配置也不同:https://github.com/Azure-Samples/active-directory-dotnet-webapp-webapi-openidconnect/blob/master/TodoListWebApp/App_Start/Startup.Auth.cs.