如何從Active Directory DirectoryEntry(SchemaClassName =「user」)對象獲取Windows用戶和域?如何從AD DirectoryEntry獲取DOMAIN USER?
用戶名稱位於sAMAccountName屬性中,但我可以在哪裏查找域名?
(因爲用戶從不同的子域我不能假設一個固定的域名。)
如何從Active Directory DirectoryEntry(SchemaClassName =「user」)對象獲取Windows用戶和域?如何從AD DirectoryEntry獲取DOMAIN USER?
用戶名稱位於sAMAccountName屬性中,但我可以在哪裏查找域名?
(因爲用戶從不同的子域我不能假設一個固定的域名。)
我在CN =分區,CN =配置包含所有域發現了一個分區容器。
當您將用戶與該分區匹配時,您可以從nETBIOSName +「\」+ sAMAccountName屬性中讀取真實的域名。
如果您使用的是System.DirectoryServices庫,你應該從一個的DirectorySearcher SearchResultsCollection。
在每個SearchResult的Properties集合中,都有一個「distinguishedname」屬性。這將包含組成目錄條目所屬域的所有DC部分。
+1:我已經做到了這一點,以獲得各種信息他們受僱的人以及所有安全團體的名單。 – RSolberg 2009-06-02 20:29:54
是的,但這對我有什麼幫助?如何從DC = Company,DC = com獲取域名「company-central」? – laktak 2009-06-03 09:59:23
這是去......但要記住一個域名往往可以在AD的正確路徑... e.g別名的正確方法。在我們的組織,域SOUTH_AMERICA實際上soa.company.com是,歐洲是eur.company.com這是由DC =歐元,DC =公司,DC = COM等爲代表,所以你可能需要一個查找表,做一個搜索在distinguishedName字符串 – davidsleeps 2009-07-01 05:11:35
不幸的是,您不會在DirectoryEntry中找到您要查找的內容。
你有sAMAccountName
這通常是類似myuser
(沒有域名)。你有distinguishedName
這就像LDAP://cn=joe myuser,cn=Users,dc=yourCompany,dc=com
。您還有一個userPrincipalName
,但通常是格式爲[email protected]
的名稱。
但不幸的是,您不會找到其中有domain\MyUser
的任何屬性。你必須把你的關於域名的信息和DirectoryEntry的sAMAccountName放在一起。
有關System.DirectoryServices中所有LDAP和WinNT屬性的更多信息和一些出色Excel表,請查看ADSI MVP Richard Mueller的Hilltop Lab網站。
馬克
這假定results
是從DirectorySearcher獲得的SearchResultCollection,但您應該能夠直接從DirectoryEntry獲取objectsid。
SearchResult result = results[0];
var propertyValues = result.Properties["objectsid"];
var objectsid = (byte[])propertyValues[0];
var sid = new SecurityIdentifier(objectsid, 0)
var account = sid.Translate(typeof(NTAccount));
account.ToString(); // This give the DOMAIN\User format for the account
爲了讓您可以在 directoryEntry.Parent
使用遞歸的DirectoryEntry域名。 然後,如果directoryEntry.SchemaClassName == "domainDNS"
你能得到的網域名稱,例如:
directoryEntry.Properties["Name"].Value
1)你可以從的DirectoryEntry中的UserPrincipalName。
2)然後,在用戶名和域名之間分割UPN。
3)然後調用GetNetBIOSName()。
public static DirectoryEntry GetDirectoryObject(string strPath)
{
if (strPath == "")
{
strPath = ConfigurationManager.AppSettings["LDAPPath"]; //YOUR DEFAULT LDAP PATH ie. LDAP://YourDomainServer
}
string username = ConfigurationManager.AppSettings["LDAPAccount"];
string password = ConfigurationManager.AppSettings["LDAPPassword"];
//You can encrypt and decrypt your password settings in web.config, but for the sake of simplicity, I've excluded the encryption code from this listing.
}
catch (Exception ex)
{
HttpContext.Current.Response.Write("user: " + username + ", LDAPAccount: "+ ConfigurationManager.AppSettings["LDAPAccount"] + ".<br /> "+ ex.Message +"<br />");
if (HttpContext.Current.User.Identity != null)
{
HttpContext.Current.Response.Write("HttpContext.Current.User.Identity: " + HttpContext.Current.User.Identity.Name + ", " + HttpContext.Current.User.Identity.IsAuthenticated.ToString() + "<br />");
HttpContext.Current.Response.Write("Windows Identity: " + WindowsIdentity.GetCurrent().Name + ", " + HttpContext.Current.User.Identity.IsAuthenticated.ToString());
}
else
{
HttpContext.Current.Response.Write("User.Identity is null.");
}
HttpContext.Current.Response.End();
}
DirectoryEntry oDE = new DirectoryEntry(strPath, username, password, AuthenticationTypes.Secure);
return oDE;
}
public static string GetNetBIOSName(string DomainName)
{
string netBIOSName = "";
DirectoryEntry rootDSE =GetDirectoryObject(
"LDAP://"+DomainName+"/rootDSE");
string domain = (string)rootDSE.Properties[
"defaultNamingContext"][0];
// netBIOSName += "Naming Context: " + domain + "<br />";
if (!String.IsNullOrEmpty(domain))
{
//This code assumes you have a directory entry at the /CN=Partitions, CN=Configuration
//It will not work if you do not have this entry.
DirectoryEntry parts = GetDirectoryObject(
"LDAP://"+DomainName+"/CN=Partitions, CN=Configuration," + domain);
foreach (DirectoryEntry part in parts.Children)
{
if ((string)part.Properties[
"nCName"][0] == domain)
{
netBIOSName += (string)part.Properties[
"NetBIOSName"][0];
break;
}
}
}
return netBIOSName;
}
public static string GetDomainUsernameFromUPN(string strUPN)
{
string DomainName;
string UserName;
if (strUPN.Contains("@"))
{
string[] ud = strUPN.Split('@');
strUPN= ud[0];
DomainName = ud[1];
DomainName=LDAPToolKit.GetNetBIOSName(DomainName);
UserName= DomainName + "\\" + strUPN;
}
else
{
UserName= strUPN;
}
return UserName;
}
public static string GetDomainNameUserNameFromUPN(string strUPN)
{
try
{
WindowsIdentity wi = new WindowsIdentity(strUPN);
WindowsPrincipal wp = new WindowsPrincipal(wi);
return wp.Identity.Name;
}
catch (Exception ex)
{
}
return "";
}
我被@laktak延伸以前的答案提供的他是什麼意思的細節。
有一個在CN=Partitions,CN=Configuration
包含它給你的cn
這是NetBIOS域名和包含distinguishedName
前綴,如果他們在這個域中的用戶將有nCName
財產所有域的分區容器。
因此,通過搜索LDAP在CN=Partitions,CN=Configuration
開始爲(objectClass=*)
和(cn
,nCName
)對每個結果存儲到地圖。
接下來LDAP查詢使用(sAMAccountName=USERIDHERE)
,並從用戶的distinguishedName
。現在,經過(cn
,nCName
)對,發現前綴從用戶distinguishedName
的nCName
,以及相應的cn
是您想要的域名。
相關:http://stackoverflow.com/questions/4249139/c-sharp-active-directory-get-domain-name-of-user – 2016-01-11 15:18:31