如何从与用户关联的活动目录管理器中获取管理员名称和电子邮件地址等详细信息?
我能够获得用户的所有详细信息:
ActiveDirectory.SearchUserinAD("ads", "sgupt257");
public static bool SearchUserinAD(string domain, string username)
{
using (var domainContext = new PrincipalContext(ContextType.Domain, domain))
{
using (var user = new UserPrincipal(domainContext))
{
user.SamAccountName = username;
using (var pS = new PrincipalSearcher())
{
pS.QueryFilter = user;
var results = pS.FindAll().Cast<UserPrincipal>();
{
foreach (var item in results)
{
File.WriteAllText("F:\\webapps\\CIS\\UserInfo.txt", item.DisplayName + item.Name + item.EmailAddress + item.EmployeeId + item.VoiceTelephoneNumber + item.Guid + item.Context.UserName + item.Sid);
}
if (results != null && results.Count() > 0)
{
return true;
}
}
}
}
}
return false;
}
感谢。
答案 0 :(得分:4)
如果要使用Principal而不是DirectorySearcher,可以在UserPrincipal对象上调用GetUnderlyingObject()
并获取DirectoryEntry。
using(var user = new UserPrincipal(domainContext))
{
DirectoryEntry dEntry = (DirectoryEntry)user.GetUnderlyingObject();
Object manager = dEntry.Properties["manager"][0];
}
答案 1 :(得分:2)
我使用DirectorySearcher从AD获取数据。 您可以通过以下方式获得经理:
DirectoryEntry dirEntry = new DirectoryEntry("LDAP://DC=company,DC=com");
DirectorySearcher search = new DirectorySearcher(dirEntry);
search.PropertiesToLoad.Add("cn");
search.PropertiesToLoad.Add("displayName");
search.PropertiesToLoad.Add("manager");
search.PropertiesToLoad.Add("mail");
search.PropertiesToLoad.Add("sAMAccountName");
if (username.IndexOf('@') > -1)
{
// userprincipal username
search.Filter = "(userPrincipalName=" + username + ")";
}
else
{
// samaccountname username
String samaccount = username;
if (username.IndexOf(@"\") > -1)
{
samaccount = username.Substring(username.IndexOf(@"\") + 1);
}
search.Filter = "(sAMAccountName=" + samaccount + ")";
}
SearchResult result = search.FindOne();
result.Properties["manager"][0];
现在您知道谁是经理,因此您可以查询有关经理的数据。
答案 2 :(得分:0)
我结合使用DirectorySearcher和PrincipalSearcher返回唯一标识符sAMAccountName,这样我就可以从AD获取所有信息
public string GetManagerId(string id)
{
string managerNetId = "Not_Found";
try
{
using (DirectorySearcher searcher = new DirectorySearcher(Context.LdapConnection))
{
//We search known user Id
searcher.Filter = "(sAMAccountName=" + id + ")";
//We search Manager Property
searcher.PropertiesToLoad.Add("manager");
SearchResult result = searcher.FindOne();
string DistingedName = result.Properties["manager"][0].ToString();
// We create domain context
PrincipalContext PrContext = new PrincipalContext(ContextType.Domain, "YourDomain.com", "OU=Users,OU=****,OU=****,OU=****,DC=*****,DC=*****");
//We define a "query-by-example" principal - here, we search for a UserPrincipal
UserPrincipal qbeUser = new UserPrincipal(PrContext);
// We define parameter for search operation
string mngt = DistingedName.Trim();
qbeUser.Surname = mngt.Substring(mngt.IndexOf("=") + 1, mngt.IndexOf(",") - 4).ToLower();
string fnm = mngt.Insert(1, "\\,");
qbeUser.GivenName = fnm.Substring(mngt.IndexOf(",") + 4, mngt.IndexOf(",") - 5).ToLower() + "*";
// create your principal searcher passing in the QBE principal
PrincipalSearcher srch = new PrincipalSearcher(qbeUser);
// find all matches
foreach (var found in srch.FindAll())
{
// We check if is realy user Manager
if (found.DistinguishedName == DistingedName)
{
managerNetId = found.SamAccountName;
}
}
return managerNetId;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return null;
}
}
public string GetManagerMail(string managerNetId)
{
try
{
using (DirectorySearcher searcher = new DirectorySearcher(Context.LdapConnection))
{
searcher.Filter = "(sAMAccountName=" + id + ")";
searcher.PropertiesToLoad.Add("mail");
SearchResult result = searcher.FindOne();
return result.Properties["mail"][0].ToString();
}
}
catch (Exception)
{
return null;
}
}