2011-02-12 88 views
7

我正在使用下面的代碼在Active Directory中驗證用戶身份,但密碼以明文形式發送。如何散列我的密碼然後發送到Active Directory?活動目錄身份驗證

DirectoryEntry entry = new DirectoryEntry(path, username, 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; 
+0

這是一個很好的問題。出於好奇,你使用了什麼AuthenticationType? – Pandincus 2011-02-12 07:19:33

+0

你是什麼意思的AuthenticationType,我使用System.DirectoryServices;名稱空間和提到的身份驗證代碼 – 2011-02-12 07:51:00

回答

6

如果您使用的是.NET 3.5,那麼我強烈建議切換到使用System.DirectoryServices.AccountManagement命名空間(閱讀所有關於它:Managing Directory Security Principals in the .NET Framework 3.5)。

許多事情都在S.DS.AM輕鬆很多 - 就像認證用戶:

PrincipalContext ctx = new PrincipalContext(ContextType.Domain); 
ctx.ValidateCredentials("test", "test", ContextOptions.SecureSocketLayer); 

安全地做到這一點的唯一方法是通過指定ContextOptions.SecureSocketLayer選擇使用SSL保護的連接來執行。

如果您不能移動到.NET 3.5和S.DS.AM,你需要檢查出AuthenticationTypes您可以在定義fourth overloaded constructorDirectoryEntry的:

DirectoryEntry entry = 
    new DirectoryEntry(path, username, pwd, 
         AuthenticationTypes.SecureSocketsLayer); 

有沒有其他辦法可以做到這一點,我害怕 - 我不認爲有什麼辦法可以讓你在客戶端以與Windwos Server/Active Directory相同的方式散列密碼,並傳入散列值...