【发布时间】:2021-06-10 21:34:23
【问题描述】:
我需要能够通过 C# 从 Windows 10 检索当前登录的 Azure AD 用户的电子邮件(技术上是 UPN)。我可以得到以下信息:
WindowsIdentity currentIdentity = WindowsIdentity.GetCurrent();
string userSid =
currentIdentity.Claims.FirstOrDefault(
u => u.Type == System.Security.Claims.ClaimTypes.PrimarySid).Value;
string username = currentIdentity.Name; // This returns "AzureAD\\TestUser"
但我真正需要的是如下电子邮件/UPN (test.user@testtenant.onmicrosoft.com):
我查看了currentIdentity 对象的所有属性,但在任何地方都看不到它,这可能吗?
【问题讨论】:
-
@PamelaPeng 我在发布后不久就注意到了这个问题,但是它抛出了一个
InvalidCastException并带有错误消息Unable to cast object of type 'System.DirectoryServices.AccountManagement.GroupPrincipal' to type 'System.DirectoryServices.AccountManagement.UserPrincipal'.
标签: c# windows azure azure-active-directory