.NET:如何在Active Directory中查找用户?
如何在Active Directory中查找用户 一些示例用户名包括:.NET:如何在Active Directory中查找用户?,.net,active-directory,.net,Active Directory,如何在Active Directory中查找用户 一些示例用户名包括: 无托吡酯 阿凡达 ian@avatopia.com ian@avatopia.local avatopia.com\ian 需要注意的是,我不知道域名 有些是失败的 using System.DirectoryServices; /// <summary> /// Gets the email address, if defined, of a user from Active Directory. ///
- 无托吡酯
- 阿凡达
- ian@avatopia.com
- ian@avatopia.local
- avatopia.com\ian
using System.DirectoryServices;
/// <summary>
/// Gets the email address, if defined, of a user from Active Directory.
/// </summary>
/// <param name="userid">The userid of the user in question. Make
/// sure the domain has been stripped first!</param>
/// <returns>A string containing the user's email address, or null
/// if one was not defined or found.</returns>
public static string GetEmail(string userid)
{
DirectorySearcher searcher;
SearchResult result;
string email;
// Check first if there is a slash in the userid
// If there is, domain has not been stripped
if (!userid.Contains("\\"))
{
searcher = new DirectorySearcher();
searcher.Filter = String.Format("(SAMAccountName={0})", userid);
searcher.PropertiesToLoad.Add("mail");
result = searcher.FindOne();
if (result != null)
{
email = result.Properties["mail"][0].ToString();
}
}
return email;
}
由于不允许您通过域,因此无法区分这两个用户
ian
ian
另一个家伙对sackoverflow感兴趣,但公认的答案是你必须这么做
首先找到的命名上下文
所需的域
我不知道“命名上下文”是什么,也不知道“必需域”是什么。我真的不想编写正则表达式来尝试将用户名解析为域名和帐户名,例如
domain.something\user-name
进入
因为我知道我会出错的。我想要在active directory中查找用户的正确、预期的方法
CodeProject上有一个不错的页面,但是你不能通过用户名查找用户信息
我希望我能给我的域控制器()一个用户名,它会找出该用户属于哪个域,与该域控制器交谈,并完成工作。这对我来说很有用
您应该能够区分不同域控制器(即域/用户名)上的不同用户,因为LDAppath将不同。据你说,你不在乎,因为你是
您正在就剥离User.Identity.Name的域/进行出价交易。但我不知道你在担心什么,你只需要把绳子切成两半,当你第一次遇到“\”的时候,是时候砍了
如果你不喜欢,你可以用“适当的方式”:
这也很好
为了username@domain风格
public static string GetDomain(string s) //untested
{
int stop = s.IndexOf("@");
return (stop > -1) ? s.Substring(stop + 1, s.Length - stop - 1) : null;
}
public static string GetLogin(string s) //untested
{
int stop = s.IndexOf("@");
return (stop > -1) ? s.Substring(0, stop) : null;
}
我不知道什么是纯.net方法。但是你可以使用win32的
domain.something
user-name
/// This is some imaginary code to show you how to use it
Session["USER"] = User.Identity.Name.ToString();
Session["LOGIN"] = RemoveDomainPrefix(User.Identity.Name.ToString()); // not a real function :D
string ldappath = "LDAP://your_ldap_path";
// "LDAP://CN=<group name>, CN =<Users>, DC=<domain component>, DC=<domain component>,..."
Session["cn"] = GetAttribute(ldappath, (string)Session["LOGIN"], "cn");
Session["displayName"] = GetAttribute(ldappath, (string)Session["LOGIN"], "displayName");
Session["mail"] = GetAttribute(ldappath, (string)Session["LOGIN"], "mail");
Session["givenName"] = GetAttribute(ldappath, (string)Session["LOGIN"], "givenName");
Session["sn"] = GetAttribute(ldappath, (string)Session["LOGIN"], "sn");
/// working code
public static string GetAttribute(string ldappath, string sAMAccountName, string attribute)
{
string OUT = string.Empty;
try
{
DirectoryEntry de = new DirectoryEntry(ldappath);
DirectorySearcher ds = new DirectorySearcher(de);
ds.Filter = "(&(objectClass=user)(objectCategory=person)(sAMAccountName=" + sAMAccountName + "))";
SearchResultCollection results = ds.FindAll();
foreach (SearchResult result in ds.FindAll())
{
OUT = GetProperty(result, attribute);
}
}
catch (Exception t)
{
// System.Diagnostics.Debug.WriteLine(t.Message);
}
return (OUT != null) ? OUT : string.Empty;
}
public static string GetProperty(SearchResult searchResult, string PropertyName)
{
if (searchResult.Properties.Contains(PropertyName))
{
return searchResult.Properties[PropertyName][0].ToString();
}
else
{
return string.Empty;
}
}
public static string GetDomain(string s)
{
int stop = s.IndexOf("\\");
return (stop > -1) ? s.Substring(0, stop + 1) : null;
}
public static string GetLogin(string s)
{
int stop = s.IndexOf("\\");
return (stop > -1) ? s.Substring(stop + 1, s.Length - stop - 1) : null;
}
public static string GetDomain(string s) //untested
{
int stop = s.IndexOf("@");
return (stop > -1) ? s.Substring(stop + 1, s.Length - stop - 1) : null;
}
public static string GetLogin(string s) //untested
{
int stop = s.IndexOf("@");
return (stop > -1) ? s.Substring(0, stop) : null;
}