首頁  >  文章  >  後端開發  >  使用 C# 的活動目錄

使用 C# 的活動目錄

PHPz
PHPz原創
2024-09-03 15:33:36485瀏覽

使用 C# 的 Active Directory 首先是組織電腦、公司使用者等。最重要的是使用者管理系統,通常用於企業網路和商業目的。公司管理員將用來組織公司的整個歷史記錄,包括電腦所屬的網路、設定檔和存取權限等。 Active Directory 是一種類似 Oracle 和 SQL 的資料庫,它有自己的查詢類型語言和基於 LDAP 的規則集。

文法:

要存取 DirectoryEntry 對象,我們需要 Active Directory 使用者名稱、密碼和伺服器位址。伺服器位址包含IP位址或LDAP的路徑,語法如下,

LDAP://domain_name.com/DC=domain_name,DC=com

Active Directory 在 C# 中如何運作?

Active Directory 對 IT 管理員確實很有幫助,它有助於組織公司使用者、桌面等,它具有完整的層次結構,屬於哪個網路、個人資料圖片顯示的內容以及誰存取儲存等等。大多數企業將其應用程式從桌面應用程式升級為支援基於表單的 LDAP 驗證的 Web 應用程式。有時,.NET 應用程式會與 Microsoft Active Directory (AD) 交互,以檢索使用者清單、搜尋群組、對使用者進行身份驗證,以及驗證哪些使用者屬於哪個 Active Directory 群組。透過使用一些方法,我們可以從我們網域內的 AD 資料庫中檢索資訊。

有多種方法,其中一種是使用 LDAP(輕量級目錄存取協定),它包含屬於命名空間 System.DirectoryServices 的類別 DirectoryEntry 和 DirectorySearch。另一種方法是使用 Active Directory (AD) 中命名空間 System.DirectoryServices.AccountManagement 下的整套類別包裝器。透過使用 LDAP 查詢,我們可以從 AD 資料庫中取得資訊。此類別允許存取整個 AD,但包裝類別允許檢索 AD 中的使用者、電腦物件、群組。 DirectoryEntry 和 DirectorySearch 物件類別比 System.DirectoryServices.AccountManagement 物件更快。

C# 的 Active Directory 的基本功能包括 System.DirectoryService 函式庫,它有助於使用 AD 函式庫例程進行查詢。 Active Directory 透過 DirectoryEntry 物件進行通訊。這些物件是與 LDAP 資料庫最重要的連接,我們可以查詢其他物件和資料夾。要存取 DirectoryEntry 對象,我們需要 Active Directory 使用者名稱、密碼和伺服器位址。伺服器位址包含IP位址或LDAP的路徑,如下所示,

LDAP://domain_name.com/DC=domain_name,DC=com

使用 C# 的 Active Directory 範例

為了與 DirectoryEntry 物件連接 Active Directory,我們需要建立安全性驗證類型的用戶,該用戶指示與 Active Directory 的安全性驗證連線。當與管理員帳戶連線時,我們可以執行管理員 Active Directory 功能,例如建立新使用者、更新使用者、刪除使用者等。

取得Directory物件的DirectoryEntry

private DirectoryEntry Reterieve_DirectoryObject( )
{
DirectoryEntry Obj_de;
Obj_de=new DirectoryEntry("LDAP://IP_Address", "admin","password", AuthenticationTypes Secure);
return _de;
}

在 C# 中從 Active Directory 取得使用者

連接到 Active Directory 後,我們需要查詢一個對象,例如取得用戶,如下所示,

private DirectoryEntry Reterieve_User(string User_Name)
{
DirectoryEntry obj_de = Reterieve_DirectoryObject( );
DirectorySearcher obj_deSearch = new DirectorySearcher();
obj_deSearch.SearchRoot = obj_de;
obj_deSearch.Filter = "(&(objectClass=user)(JOHNAccountName=" + User_Name + "))";
obj_deSearch.SearchScope = SearchScope.Subtree;
SearchResult getPath = obj_deSearch.FindOne();
if (!(getPath == null))
{
obj_de = new DirectoryEntry(getPath.Path, "administrator", "password", AuthenticationTypes.Secure);
return obj_de;
}
else
{
return null;
}
}

上面的程式碼解釋如何根據使用者的登入憑證從 Active Directory 檢索使用者。我們需要使用括號中包含的特定 Active Directory 查詢語言,它包含像「JohnAccountName」這樣的名稱和像 Active Directory 中一樣的使用者名稱。一旦找到 DirectoryEntry,就可以編碼新的 DirectoryEntry 對象,該對象會連結到結果並在連接中使用管理員的登入詳細資訊。

建立驗證使用者

要為 Active Directory 建立經過驗證的用戶,我們需要將有效的 LDAP 路徑字串傳遞給 DirectoryEntry 類別建構函數,它遵循 LDAP://Doamin_name 的格式。我們來看看下面的方法,

private bool AuthenticateUser(string domain_name, string user_name, string password)
{
bool result = false;
try
{
DirectoryEntry obj_de = new DirectoryEntry("LDAP://" + domainName, userName, password);
DirectorySearcher obj_dsearch = new DirectorySearcher(obj_de);
SearchResult _sResult = null;
sResult = obj_dsearch.FindOne();
result = true;
}
catch
{
result = false;
}
return result;
}

使用 C# 變更 Active Directory 中的使用者詳細資料

在C#中更改Active Directory物件的屬性在C#中很簡單,首先存取DirectoryEntry屬性欄位並根據需要變更值,然後檢查它是否為空,最後呼叫函數ComminChanges 執行並儲存所做的變更。讓我們來看看下面的程式碼,它顯示了更改使用者的 name 屬性,

DirectoryEntry obj_de = Reterieve_User ("smith.rio");
if (obj_de!= null)
{
if (obj_de.Properties["displayName"] != null && obj_de.Properties["displayName"].Value != null)
{
de.Properties["displayName"].Value = "Smith, Rio (Welcome)";
de.CommitChanges();
}
}

The above code describes the CommitChanges(), which will save the changes made in the Active Directory. The most important thing is that whatever changes are made will not be immediately visible in the applications of Active Directory like users of Active Directory and computers in the control panel it takes around 5-30 minutes to visible during changes because it needs to synchronize over the servers all through the network.

Querying Multiple Users in Active Directory with C# ASP .NET

The code above explained was to query the single DirectoryEntry object, whereas if we required for the list of objects we need to use the type SearchResultCollection joined with obj_deSearch, to search out all the things instead of finding one,

SearchResultCollection findUsers = retrieve_allUsers();
if (findUsers!= null && findUsers.Count > 0)
{
foreach (SearchResult getUser in findUsers)
{
DirectoryEntry obj_de = getUser.GetDirectoryEntry();
}
}

Conclusion

In this article, I have explained about the Active Directory and how to retrieve details of users, system usage, groups, and also to authenticate the user. By using some classes, we can easily retrieve the details from the active directory (AD) database. Active Directory with C# is a foremost tool for enterprise networks and for businesses. When designing with web applications which suit for desktop applications to the web to make powerful organizations.

以上是使用 C# 的活動目錄的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn