首页 > 解决方案 > 在 Web API 控制器中从 Azure AD 获取当前登录的用户详细信息

问题描述

NET Core 3.0 Web API,之前我使用的是 Windows 身份验证。所以在我的控制器中,这是我获取当前登录用户详细信息的方式。

PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(ctx, User.Identity.Name);

我已经在 Azure 门户和我的应用程序中为基于 Azure AD 的身份验证设置了所有内容。因此,当我启动应用程序时,它登录成功并返回到我的页面,因为我在门户中设置了replyurl

但是在返回时,我在ctx变量中没有得到任何东西。我真的相信在使用 Azure AD 时,这可能是实现这一目标的其他方式。请帮我解决这个问题。

标签: azureasp.net-web-api.net-coreazure-active-directoryasp.net-core-webapi

解决方案


您应该能够获取详细信息

ViewBag.Name = ClaimsPrincipal.Current.FindFirst(ClaimTypes.Name).Value;

推荐阅读