簡體   English   中英

針對Active Directory的SimpleBind身份驗證

[英]SimpleBind Authentication against Active Directory

我正在嘗試使用以下代碼針對Active Directory(AD DS)驗證登錄憑據:

using (var context = new PrincipalContext(ContextType.Domain, ipAddress))
{
    Console.WriteLine("Connected to {0}:", context.ConnectedServer);
    context.ValidateCredentials(username, password);
}

其中ipAddress是主域控制器的地址。 但是,在嘗試讀取context.ConnectedServer時,這會出現以下錯誤:

System.DirectoryServices.DirectoryServicesCOMException(0x8007052E):用戶名或密碼不正確。

除了這個問題,我還有以下限制:

  • 生產環境可能在域上,也可能不在域上。

  • 客戶端不想輸入任何特權憑證來查詢目錄。

由於第二個約束,我試圖執行一個SimpleBind,但是運氣不好:

using (var context = new PrincipalContext(ContextType.Domain, 
                                          ipAddress, 
                                          null, 
                                          ContextOptions.SimpleBind, 
                                          usernameToValidate, 
                                          password))

基於這些限制,如何針對Active Directory進行身份驗證?

我可以使用以下代碼進行身份驗證:

using (var context = new PrincipalContext(ContextType.Domain, ipAddress))
{
    // NOTE! Username must be of the format domain\username
    return context.ValidateCredentials("domain\someuser", password, ContextOptions.SimpleBind);
}

關鍵部分是用短域名為用戶名加上前綴。 一旦做到這一點,並在對ValidateCredentials的調用中而不是在上下文構造函數中指定SimpleBind,它就可以正常工作。

暫無
暫無

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

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