我们需要将AzureAD身份验证流程集成到现有项目中。所以我在here找到了示例代码。它基本上包含两个Web项目。作为客户端的MVC项目和作为服务器的Web API。
我们使用JWT令牌中的“upn”声明来唯一标识登录用户。我运行了示例代码,并能够在Azure AD中验证用户身份。当我检查我在Jwt.io从AAD获得的Access令牌时,声明部分不包含“upn”但我能够使用ClaimsPrincipal.Current.FindFirst(ClaimTypes.Upn)
检索客户端上的“upn”。添加此访问令牌作为承载授权标头并调用web api。在这里,我无法使用ClaimsPrincipal.Current.FindFirst(ClaimTypes.Upn)
来回复“upn”声明。此声明的值为null。我是否需要进行任何其他配置才能进入服务器端。截至目前,我只能在客户端检索upn。
答案 0 :(得分:1)
有问题的示例项目:
以应用程序的身份调用Web API,而不是用户的身份
在TodoListController的第95行,它获取令牌:
result = await authContext.AcquireTokenAsync(todoListResourceId, clientCredential);
它正在调用API本身,因此它是一个仅限应用程序的调用,而不是委托调用。因此没有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));