![](/img/trans.png)
[英]ASP.NET MVC - Authenticate users against Active Directory automatically?
[英]active directory users on asp.net
我想使用我們公司的Active Directory創建目錄Intranet網站。 到目前為止,我已經明白了,但是當我在調試模式下運行時,代碼在searchResultCollection....search.findAll();
中斷searchResultCollection....search.findAll();
顯示:
[DirectoryServicesCOMException(0x80072020):發生操作錯誤。
我嘗試將IIS asp.net模擬更改為啟用,但收到HTTP錯誤500.24。 我的用戶名具有對Active Directory的讀取權限。 是我缺少某些東西,還是可以將我指向正確的方向? 我到處都是我被困住的地方。
在此先感謝您的幫助。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.DirectoryServices;
using System.Web.Security;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
GetADUsers();
}
public void GetADUsers()
{
DirectoryEntry myLdap = new DirectoryEntry("LDAP://OU=Nix,DC=systems,DC=com");
DirectorySearcher search = new DirectorySearcher(myLdap);
search.CacheResults = true;
search.SearchScope = SearchScope.Subtree;
search.Filter = "(objectlass=person)";
SearchResultCollection allResults = search.FindAll();
foreach (SearchResult sr in allResults)
{
Response.Write(sr.Properties["name"].ToString());
}
}
您可以使用PrincipalSearcher
和“按示例查詢”主體進行搜索:
// create your domain context
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{
// define a "query-by-example" principal - here, we search for a UserPrincipal
// and with the first name (GivenName) of "Bruce" and a last name (Surname) of "Miller"
UserPrincipal qbeUser = new UserPrincipal(ctx);
qbeUser.GivenName = "Bruce";
qbeUser.Surname = "Miller";
// create your principal searcher passing in the QBE principal
PrincipalSearcher srch = new PrincipalSearcher(qbeUser);
// find all matches
foreach(var found in srch.FindAll())
{
// do whatever here - "found" is of type "Principal" - it could be user, group, computer.....
}
}
如果您還沒有-絕對請閱讀MSDN文章.NET Framework 3.5中的 “ 管理目錄安全性主體”,它很好地展示了如何充分利用System.DirectoryServices.AccountManagement
中的新功能。 或參閱System.DirectoryServices.AccountManagement命名空間上的MSDN文檔 。
當然,根據您的需要,您可能希望在創建的“按示例查詢”用戶主體上指定其他屬性:
DisplayName
(通常:名字+空格+姓氏) SAM Account Name
-您的Windows / AD帳戶名 User Principal Name
-您的“ username@yourcompany.com”樣式名稱 您可以在UserPrincipal
上指定任何屬性,並將它們用作PrincipalSearcher
“按示例查詢”。
自我重新啟動后,我再次測試了它是否可以正常運行,然后添加了其余代碼以顯示在gridview中。
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
GetADUsers();
}
public void GetADUsers()
{
DirectoryEntry myLdap = new DirectoryEntry("LDAP://OU=Nix,DC=systems,DC=com");
DirectorySearcher search = new DirectorySearcher(myLdap);
search.CacheResults = true;
search.SearchScope = SearchScope.Subtree;
search.Filter = "(objectlass=person)";
SearchResultCollection allResults = search.FindAll();
search.PropertiesToLoad.Add("samaccountname");
Grid1.DataSource = allResults;
Grid1.DataBind();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.