[英]Generic Authentication Call to Active Directory in C#
我想擁有一個從Active Directory進行身份驗證的干凈C#類。
它應該非常簡單,只需詢問憑據並檢查其是否符合AD的預期。
我負責許多C#應用程序,我希望它們全部使用同一類。
有人可以提供此類的干凈代碼示例嗎? 它應具有良好的錯誤處理能力,並具有良好的注釋,尤其應要求提供憑據,而不要嘗試讀取用戶是否已針對另一個應用程序登錄到AD。 (這是安全性要求,因為某些應用程序用於共享計算機區域:具有多個角色和不同權限級別的人員可能使用同一台計算機,而忘記在會話之間注銷)
http://support.microsoft.com/kb/316748
public bool IsAuthenticated(String domain, String username, String pwd)
{
String domainAndUsername = domain + "\\" + username;
DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, pwd);
try
{ //Bind to the native AdsObject to force authentication.
Object obj = entry.NativeObject;
DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = "(SAMAccountName=" + username + ")";
search.PropertiesToLoad.Add("cn");
SearchResult result = search.FindOne();
if(null == result)
{
return false;
}
//Update the new path to the user in the directory.
_path = result.Path;
_filterAttribute = (String)result.Properties["cn"][0];
}
catch (Exception ex)
{
throw new Exception("Error authenticating user. " + ex.Message);
}
return true;
}
誠然,我沒有針對AD進行編程的經驗,但是下面的鏈接似乎可以解決您的問題。
出於某些原因,您不能使用Windows集成身份驗證 ,並且不打擾用戶輸入用戶名和密碼? 在可能的情況下,這同時也是最有用和最安全的解決方案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.