C# Windows身份验证User.Identity.Name到电子邮件地址

C# Windows身份验证User.Identity.Name到电子邮件地址,c#,active-directory,windows-authentication,C#,Active Directory,Windows Authentication,我已经尝试过使用,但仍然存在一个问题: 我知道如何询问John Smith的电子邮件地址,但我得到的身份验证名称类似于INTRA\\JohnSmith3或DEP21\\JohnSmith 如何将INTRA\\JohnSmith3或DEP21\\JohnSmith映射到AD中正确的John Smith?您从Windows身份验证获得的是SAM帐户名。您需要在Active Directory中查找此信息 您可以查询Active Directory中的以下用户: (&(objectCatego

我已经尝试过使用,但仍然存在一个问题:

我知道如何询问John Smith的电子邮件地址,但我得到的身份验证名称类似于INTRA\\JohnSmith3或DEP21\\JohnSmith


如何将INTRA\\JohnSmith3或DEP21\\JohnSmith映射到AD中正确的John Smith?

您从Windows身份验证获得的是SAM帐户名。您需要在Active Directory中查找此信息

您可以查询Active Directory中的以下用户:

(&(objectCategory=person)(objectClass=user)(sAMAccountName=JohnSmith3))
代码:

string filter = "(&(objectCategory=person)"
     + "(objectClass=user)"
     + "(sAMAccountName=" + samAccountName + "))";
DirectorySearcher search = new DirectorySearcher(myLdapConnection);
search.Filter = filter;
SearchResult result = search.FindOne();
DirectoryEntry de = result.GetDirectoryEntry();

您从Windows身份验证获得的是SAM帐户名。您需要在Active Directory中查找此信息

您可以查询Active Directory中的以下用户:

(&(objectCategory=person)(objectClass=user)(sAMAccountName=JohnSmith3))
代码:

string filter = "(&(objectCategory=person)"
     + "(objectClass=user)"
     + "(sAMAccountName=" + samAccountName + "))";
DirectorySearcher search = new DirectorySearcher(myLdapConnection);
search.Filter = filter;
SearchResult result = search.FindOne();
DirectoryEntry de = result.GetDirectoryEntry();

如果我省略了域,DEP21\\JohnSmith和HQ\\JohnSmith是否指向同一个AD条目?SAM帐户名不包括域。您可以在不执行
FindOne()
而是执行
FindAll()
的情况下执行搜索,然后在域上筛选结果。要了解如何找到给定用户的域,请查看SO上的另一个问题:最好从user.Identity.name中提取域名,并使用它连接到该域的广告?如果我省略该域,DEP21\\JohnSmith和HQ\\JohnSmith不是指向同一广告条目吗?SAM帐户名不包括该域。您可以在不执行
FindOne()
而是执行
FindAll()
的情况下执行搜索,然后在域上筛选结果。要了解如何找到给定用户的域,请查看SO上的另一个问题:最好从user.Identity.name中提取域名,并使用它连接到该域的广告?