如何在 C# 中从 Active Directory 中获取当前登录用户的可分辨名称?
最佳答案
检查以下代码段。您已从 IPrincipal 传递给 Identity.Name
.我假设用户已经在 Active Directory 中进行了身份验证(即使用标准 IIS 授权方法)。
private string GetUserName(string identity)
{
if (identity.Contains("\\"))
{
string[] identityList = identity.Split('\\');
return identityList[1];
}
else
{
return identity;
}
}
public string GetUserDn(string identity)
{
var userName = GetUserName(identity);
using (var rootEntry = new DirectoryEntry("LDAP://" + adConfiguration.ServerAddress, null, null, AuthenticationTypes.Secure))
{
using (var directorySearcher = new DirectorySearcher(rootEntry, String.Format("(sAMAccountName={0})", userName)))
{
var searchResult = directorySearcher.FindOne();
if (searchResult != null)
{
using (var userEntry = searchResult.GetDirectoryEntry())
{
return (string)userEntry.Properties["distinguishedName"].Value;
}
}
}
}
return null;
}
关于c# - 从当前登录用户的 Active Directory 中获取专有名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10428495/