[英]How to detect Active Directory path in c#
我一直在互聯網上搜索以學習如何與Active Directory進行交互。 我找到以下代碼,但是我想知道ActiveDirectoryPath
必須是什么?
歡迎對代碼進行任何簡短描述
DirectoryEntry entry = new DirectoryEntry(ActiveDirectoryPath);
DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = String.Format("(&(objectCategory=group)(cn={0}))", activeDirectoryGroup);
search.PropertiesToLoad.Add("distinguishedName");
SearchResult searchResult = search.FindOne();
if (searchResult == null)
return new HashSet<User>();
DirectoryEntry group = searchResult.GetDirectoryEntry();
Hashtable searchedGroups = new Hashtable();
return GetUsersInGroup(group.Properties["distinguishedName"].Value.ToString(), searchedGroups, path);
通過檢測Active Directory路徑無法完全弄清您的意思-確實沒有“當前” AD路徑或其他任何內容; 文件系統中沒有“當前目錄”。
您可以通過檢查LDAP://RootDSE
條目並在此處查找defaultNamingContext
來確定系統默認路徑:
using (DirectoryEntry deRoot = new DirectoryEntry("LDAP://RootDSE"))
{
if (deRoot.Properties["defaultNamingContext"] != null)
{
string defaultNamingContext =
deRoot.Properties["defaultNamingContext"].Value.ToString();
}
}
或者,您可以僅從Active Directory中檢索當前登錄的用戶,並檢查其LDAP路徑(此代碼適用於.NET 3.5及更高版本(具有新的System.DirectoryServices.AccountManagement
命名空間)):
using System.DirectoryServices.AccountManagement;
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{
UserPrincipal currentUser = UserPrincipal.Current;
string userLdapPath = currentUser.DistinguishedName;
}
這將返回用戶的完整LDAP路徑,其中包含在其中創建用戶的“容器”-類似於:
LDAP://CN=User Name,OU=SomeOU,DC=YourCompany,DC=Com
此處OU=SomeOU,DC=YourCompany,DC=Com
部分是該用戶所在的Active Directory中的“路徑”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.