本文介绍了获取专有名称从Active Directory中的当前登录的用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经搜索周围,但无法找到任何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 的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中的当前登录的用户
发布评论