获取专有名称从Active Directory中的当前登录的用户 [英] Get distinguished name from Active Directory of currently logged in user
问题描述
我已经搜索周围,但无法找到任何code段吧。
I have searched around but could not find any code snippet for it.
我怎样才能从当前登录的用户在C#中的Active Directory中的专有名称?
How can I get the distinguished name from Active Directory of the currently logged in user in C#?
不幸的是我还没有找到一个解决办法..也许有些你以前已经做到了这一点,或有一个片段而不是吗?
Unfortunately I have not found a solution yet.. Maybe some of you has done this before or has a snippet which does it?
推荐答案
检查下面的代码片段。你必须传递给 Identity.Name
的<一个href="http://msdn.microsoft.com/en-us/library/system.security.principal.iprincipal.aspx">IPrincipal.我假设用户已经通过身份验证在Active Directory中(即使用标准的IIS授权方法)。
Check following snippet. You have pass to Identity.Name
from IPrincipal. I assume that the user is already authenticated in Active Directory (ie. using standard IIS authorization methods).
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;
}
这篇关于获取专有名称从Active Directory中的当前登录的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!