2008-11-19 46 views
0

我已經遵循了Microsoft的ADAM分步指南,並在本地計算機上設置了ADAM實例。我試圖使用「Mary Baker」帳戶進行身份驗證,但每當我在下面的if (entry.Guid != null)一行上收到COM異常時。該例外狀態表明存在未知的用戶名或密碼錯誤。帶有ADAM用戶和簡單綁定的ADAM身份驗證

但是,我可以使用ldp實用程序連接到ADAM併成功執行簡單的綁定 - 因此我知道用戶名都存在,並且我擁有正確的密碼。

此外,我已將用戶的msDS-UserAccountDisabled屬性設置爲false,並將該用戶添加到Administrators和Readers角色。

有什麼想法?

path = "LDAP://localhost:50000/O=Microsoft,c=US"; 
    userId = "CN=Mary Baker,OU=ADAM users,"; 
    password = "[email protected]"; 

    DirectoryEntry entry = new DirectoryEntry(path, userId, password, AuthenticationTypes.None); 
    if (entry.Guid != null) 
     LoadWelcomeScreen(); 

謝謝。

回答

0

我的名字是ADAM,我不贊同這種認證。

(笑,不好意思,不得不這樣做)

0

我沒有使用ADAM或的System.DirectoryServices,但我確實有LDAP和AD經驗;希望以下內容適用。

我從來沒有見過以前那種格式的用戶ID。 (看起來像某種相對DN,如後面的逗號所示?)您是否嘗試將用戶標識指定爲完整的DN(如標準LDAP所要求的),或者將用戶標識指定爲裸用戶名(如果ADAM支持)?

當診斷這樣的網絡協議問題時(看我的程序是否在做我認爲我要做的事情,看看它在做什麼和正在運行的程序相比),我發現它有助於運行Wireshark無法運作和運作的操作,看看它們有何不同。如果您從未使用過Wireshark,希望不會太難:

  1. 下載,安裝並啓動軟件。
  2. 在捕獲下,單擊選項。
  3. 將接口設置爲localhost/loopback或以太網接口。 (我不認爲這種環回在Windows上按預期工作,您可能希望選擇以太網接口並在您的C#代碼中更新您的LDAP URL以使用您的主機名而不是本地主機。)
  4. 在捕獲過濾器下,輸入「tcp port 50000」(不含引號)。
  5. 單擊開始,運行連接操作,然後轉到捕獲菜單下,然後單擊停止。

Wireshark的可分析的協議你,所以你不必是熟悉協議的細節自己,雖然知道得越多,就越容易解釋所有的細節。您可以啓動Wireshark的幾個實例來輕鬆比較兩種不同的捕獲(您的代碼和LDP)。

1

ADAM將用戶的唯一標識符存儲在user類的displayName屬性中。它們需要在ADAM實例中是唯一的,以便用戶進行身份驗證。如果兩個用戶的displayName屬性設置爲'jsmith',那麼這兩個用戶都無法在ADAM中進行身份驗證。

使用ldp實用程序查詢Mary Baker的displayName。它可能是'mbaker'之類的東西。使用該值作爲給定代碼中的userId。

1

感謝瑞安爲您提示displayName。將我的測試課程發佈到我本地的ADAM實例上,供任何可能感興趣的人使用。

[TestMethod] 
    public void CreateUserAccount() 
    { 
     var username = "amurray"; 
     var password = "ADAMComplexPassword1234"; 
     var firstname = "Andy"; 
     var lastname = "Murray"; 

     const AuthenticationTypes authTypes = AuthenticationTypes.Signing | 
               AuthenticationTypes.Sealing | 
               AuthenticationTypes.Secure; 

     var ldapPath = "LDAP://localhost:389/OU=MyProject,OU=Applications,DC=Company,DC=ADAM"; 
     using (var dirEntry = new DirectoryEntry(ldapPath, "MyPC\\adamuser", "Password1!", authTypes)) 
     { 
      DirectoryEntry user = null; 
      const int ADS_PORT = 389; 
      const long ADS_OPTION_PASSWORD_PORTNUMBER = 6; 
      const long ADS_OPTION_PASSWORD_METHOD = 7; 
      const int ADS_PASSWORD_ENCODE_CLEAR = 1; 

      try 
      { 
       user = dirEntry.Children.Add(string.Format("CN={0} {1}", firstname, lastname), "user"); 
       user.Properties["displayName"].Value = username; 
       user.Properties["userPrincipalName"].Value = username; 
       user.Properties["msDS-UserAccountDisabled"].Value = false; 
       user.Properties["msDS-UserDontExpirePassword"].Value = true; 
       user.CommitChanges(); 
       var userid = user.Guid.ToString(); 

       // Set port number, method, and password. 
       user.Invoke("SetOption", new object[]{ADS_OPTION_PASSWORD_PORTNUMBER,ADS_PORT}); 
       user.Invoke("SetOption", new object[]{ADS_OPTION_PASSWORD_METHOD,ADS_PASSWORD_ENCODE_CLEAR}); 

       user.Invoke("SetPassword", new object[] {password}); 
       user.CommitChanges(); 
       user.Close(); 
      } 
      catch (Exception e) 
      { 
       var msg = e.GetBaseException().Message; 
       Console.WriteLine(e); 
       System.Diagnostics.Debug.Print(msg); 
      }     
     } 
    } 


    [TestMethod] 
    public void TestUserAuthentication() 
    { 
     try 
     { 
      var ldsContext = new PrincipalContext(ContextType.ApplicationDirectory, "localhost:389", 
                "OU=MyProject,OU=Applications,DC=Company,DC=ADAM", 
                ContextOptions.SimpleBind); 

      // Returns true if login details are valid 
      var isValid = ldsContext.ValidateCredentials("amurray", "ADAMComplexPassword1234", ContextOptions.SimpleBind); 
     } 
     catch (Exception e) 
     { 
      var msg = e.GetBaseException().Message; 
      Console.WriteLine(e); 
      System.Diagnostics.Debug.Print(msg); 
     } 
    }