C# 获取.NET web应用程序中登录用户的UPN或电子邮件
我不是.NET开发人员,我有一种感觉,这对于以下人员来说是微不足道的: 我有一个C#web应用程序,它使用户可以访问登录用户的用户凭据。当前,它使用来自的SIDC# 获取.NET web应用程序中登录用户的UPN或电子邮件,c#,.net,active-directory,C#,.net,Active Directory,我不是.NET开发人员,我有一种感觉,这对于以下人员来说是微不足道的: 我有一个C#web应用程序,它使用户可以访问登录用户的用户凭据。当前,它使用来自的SID System.Security.Principal.WindowsIdentity.GetCurrent().User.Value 我需要获取用户UPN登录名或电子邮件地址(在active directory中定义),而不是SID。GetCurrent()返回WindowsIdentity类型的对象;查看WindowsIdentit
System.Security.Principal.WindowsIdentity.GetCurrent().User.Value
我需要获取用户UPN登录名或电子邮件地址(在active directory中定义),而不是SID。GetCurrent()返回WindowsIdentity类型的对象;查看WindowsIdentity成员的详细信息:
我看不到任何东西,看起来它会给我的UPN或电子邮件在那里。我如何提取这些信息以供使用,可以将SID输入其他函数,也可以首先调用其他函数。尝试:
System.Security.Principal.WindowsIdentity.GetCurrent().Name
要使用目录搜索器查询active directory,您需要执行以下操作(完全未经测试的代码): 同时(.NET 3.5)这是一个单线:
System.DirectoryServices.AccountManagement.UserPrincipal.Current.EmailAddress
用于电子邮件,或
System.DirectoryServices.AccountManagement.UserPrincipal.Current.UserPrincipalName
对于UPN。文档中说“获取用户的Windows登录名”-这将返回NT样式名还是UPN样式名?我知道微软很多年前说过UPN将成为识别用户的新方法,但根据我的经验,几乎所有东西都是靠NT风格的凭据工作的——用户可以使用UPN或NT风格的登录来登录此网站,因此我不能依赖用户使用的相同形式。只是做了一个快速测试,并使用System.Security.Principal.WindowsIdentity.GetCurrent()。Name从此返回DOMAIN\UserName,您可以将其馈送到DirectorySearcher对象中,以获取该特定用户的更多详细信息。对于DirectorySearcher,请参见和。这太棒了!我不知道AccountManagement名称空间-这正是我要查找的名称空间。名称空间通常为null。
System.DirectoryServices.AccountManagement.UserPrincipal.Current.UserPrincipalName