簡體   English   中英

從C#對ADAM用戶進行ADAM身份驗證-無法綁定

[英]Authenticating an ADAM user against ADAM from C# - cannot bind

我已經設置了ADAM實例並添加了一些測試用戶。 從c#中,我可以使用Windows帳戶綁定到ADAM,但不能使用ADAM用戶之一綁定。 (我可以在ldp中成功綁定adam用戶),並且通過將msDS-UserAccountDisabled屬性設置為false來確保已啟用用戶。 當我用Windows帳戶綁定時,我可以成功搜索並帶回ADAM用戶的屬性,但是我仍在努力對其進行身份驗證,當我嘗試與ADAM用戶帳戶綁定時,出現錯誤:

錯誤:System.Runtime.InteropServices.COMException(0x8007052E):登錄失敗:用戶名未知或密碼錯誤。 在System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)

這是我正在使用的代碼:

string userName = txtUserName.Text;
string password = txtPassword.Text;
string ADConnectionString = "LDAP://localhost:389/CN=sandbox,DC=ITOrg";
DirectoryEntry entry = new DirectoryEntry(ADConnectionString);

entry.Username = "myComputer\\Administrator";
entry.Password = "myPassword";
try 
{
DirectorySearcher searcher = new DirectorySearcher(entry);
searcher.Filter = "(&(objectClass=user)(CN=" + userName + "))";
SearchResultCollection result = searcher.FindAll();
if (result.Count > 0)
{
    //bind with simple bind
    using (DirectoryEntry de = new DirectoryEntry(result[0].Path, userName, password,AuthenticationTypes.None))
    {
         if (de.Guid != null) // this is the line where it dies
         {
              Label1.Text = "Successfully authenticated";
              Label2.Text = result[0].Properties["displayName"][0].ToString();
              Label3.Text = result[0].Properties["telephoneNumber"][0].ToString();
          } else 
          {
             Lable1.Text = "Unable to Authenticate";
          }
     }
}
else
{
    Lable1.Text = "UserName :" + userName + " not found"; 
}
} catch(Exception ex)
{
     Label1.Text = "Error searching: " + ex.ToString();
}

在此先感謝您的幫助,不勝感激!

這可能是用戶名格式問題。 在SDS中對ADAM用戶進行身份驗證時,必須使用LDAP簡單綁定並使用ADAM支持的名稱格式。 從技術上講,ADAM還允許您使用Digest auth,但這在SDS中不可用(僅SDS.Protocols),因此不適用於您的代碼方法。

您使用的是簡單綁定,因為您有AuthenticationTypes.None,所以沒有問題。 然后可能是錯誤的部分是用戶名格式。

ADAM接受用戶的完整DN,他們的displayName(如果設置且唯一)和/或userPrincipalName(如果設置且唯一)作為“可綁定”用戶名,因此從用戶的完整DN開始,看看是否可行。 如果是這樣,您也可以嘗試其他用戶名值。 請注意,您可以將所需的displayName或userPrincipalName放在ADAM中。 沒有驗證。 只要確保值是唯一的即可。

如果您真的想對ADAM進行某種類型的綁定身份驗證,則可以通過使用.NET 3.5中PrincipalContext的ValidateCredentials方法來獲得更好的性能和擴展性。

此類內容經常在http://www.directoryprogramming.net的論壇中進行記錄和討論,並且由於它是我的網站,所以我經常光顧此地。 :)一位朋友給我發送了這篇文章,否則我將再也看不到。

暫無
暫無

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

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