簡體   English   中英

使用C#根據LDAP對用戶進行身份驗證

[英]Using C# to authenticate user against LDAP

我正在使用DirectorySearcher在LDAP服務器中搜索用戶條目。

DirectoryEntry de = new DirectoryEntry();
de.Path = "LDAP://myserver/OU=People,O=mycompany";
de.AuthenticationType = AuthenticationTypes.None;

DirectorySearcher deSearch = new DirectorySearcher();

deSearch.SearchRoot = de;
deSearch.Filter = "(uid=" + model.UserName + ")";

SearchResult result = deSearch.FindOne();

我能夠在結果變量中得到預期的輸出。
但是,如果我嘗試通過在目錄條目中提供密碼來驗證同一用戶,我總是會收到以下錯誤。

“用戶名或密碼不正確。”

DirectoryEntry entry = new DirectoryEntry("LDAP://myserver/OU=People,O=mycompany", username, password);
DirectorySearcher search = new DirectorySearcher(
    entry,
    "(uid=" + username + ")",
    new string[] { "uid" }
);

search.SearchScope = System.DirectoryServices.SearchScope.Subtree;
SearchResult found = search.FindOne();   ->>>>>this is where I get wrong credential error.

用戶名和密碼適用於我要驗證的用戶。

任何人都可以告訴我這里我做錯了什么或如何調試這個。

此行中的用戶名,密碼:

DirectoryEntry("LDAP://myserver/OU=People,O=mycompany", username, password);

應該是具有目錄查找權限的帳戶。 它可以是服務帳戶或測試目的嘗試與您自己。 這不應該是您嘗試進行身份驗證的用戶/通行證。

如果要進行身份驗證,可以使用PrincipalContext使用以下步驟:

using(var context = new PrincipalContext(ContextType.Domain, "mydomain", "mydomain\serviceAcct", "serviceAcctPass")) {
 //Username and password for authentication.
 return context.ValidateCredentials(username, password); 
}

“serviceAcct”=域用戶中具有目錄查找權限的帳戶。 “serviceAcctPass”=該服務帳戶的密碼。 正如我所說,對於測試,您可以嘗試使用自己的用戶/傳遞上下文。

另外,請確保提供的用戶名具有“domain \\ username”或“username @ domain”格式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM