2

我在我的C#web應用程序中使用DirectoryEntry方法設置LDAP登錄。下面的代碼是我到目前爲止。這將讓任何擁有AD帳戶的人登錄。我需要將其限制在名爲「commonusers」的組中。檢查用戶登錄對AD組

public Boolean ValidateUser(string userName, string password) 
    { 
     string path = "LDAP://domain.company.org"; 
     DirectoryEntry dirEntry = new DirectoryEntry(path, userName, password, AuthenticationTypes.Secure); 
     try 
     { 
      DirectorySearcher dirSearcher = new DirectorySearcher(dirEntry); 
      dirSearcher.FindOne(); 
      return true; 
      // If it returns the data then the User is validated otherwise it will automatically shift to catch block and return false 
     } 
     catch 
     { 
      return false; 
     } 

登錄按鈕使用下面的代碼:

protected void Button1_Click(object sender, EventArgs e) 
    { 
     { 
      Boolean boolresult = ValidateUser(TextBox_username.Text, TextBox_password.Text); 
      if (boolresult) 
      { 
       Label_loginstatus.Text = "Redirecting"; 

       Response.Redirect("medsearch.aspx"); 
      } 
      else 
      { 
       Label_loginstatus.Text = "Invalid username/password! Please try again."; 
      } 
     } 
    } 

是否有可能增加一個檢查功能的用戶佔了「commonusers」組到這些功能呢?

回答

0

如果在.NET 4中,您可以使用此方法,您可以在那裏進行的try/catch做:

 private bool ValidateAgainstADAndGroup(string username, string password, string groupname) 
       { 
        var ok = false; 
        using (var pc = new PrincipalContext(ContextType.Domain, "mydomain.lan")) 
        { 
         if (pc.ValidateCredentials(username, password)) 
         { 
          //User is alright 
          using (var searcher = new PrincipalSearcher(new UserPrincipal(pc))) 
          { 
           searcher.QueryFilter.SamAccountName = username; 
           Principal u = searcher.FindOne(); 
           foreach (Principal p in u.GetGroups()) 
           { 
            if (p.Name == groupname) 
            { 
             //User is in group 
             ok= true; 
            } 
           } 
          } 
         } 
        } 

        return ok; 
       } 

你可以改變返回兩個類型s的錯誤:NotAuthenticated或Authenticated - 但不在組

+0

有沒有需要添加的參考去這條路線?我添加了這個代碼,並且PrincipalContext,ContextType,PrincipalSearcher,UserPrincipal和Principal都出現了錯誤。 –

+2

System.DirectoryServices.AccountManagement - 睡前閱讀這裏:http://msdn.microsoft.com/en-us/library/system.directoryservices.accountmanagement(v = vs.110).aspx – Steen

+0

因此,此代碼工作篩選出用戶不在組中,但它允許指定組中的用戶無需密碼登錄... –

0

你想捕捉的FindOne的結果()調用,以尋找任何屬性值你所關心的:

DirectoryEntry dirEntry = new DirectoryEntry(path, userName, password, AuthenticationTypes.Secure); 
object obj = de.NativeObject; // This will force the authentication 

// Search for the user's directory entry 
DirectorySearcher dirSearcher = new DirectorySearcher(dirEntry); 
dirSearcher.Filter = "(SAMAccountName=" + userName + ")"; 
dirSearcher.PropertiesToLoad.Add("member"); 
SearchResult searchResult = dirSearcher.FindOne(); 

if (searchResult != null) 
{ 
    if (searchResult.Properties["member"] != null && searchResult.Properties["member"].Contains("commonusers")) 
    { 
     return true; 
    } 
} 

return false; 

有關如何將數據從Active Directory中的詳細信息,我建議您訪問www.selfadsi.org

+0

所以我把這個代碼到位,現在它不會讓成員或非成員通過。我現在需要更改我的按鈕的代碼嗎? –

+1

您的按鈕點擊事件代碼應該沒問題。我會遍歷ValidateUser代碼並確認searchResult有一個值,然後查看searchResult.Properties [「member」]的實際值是什麼。請注意,您可以嘗試檢索的另一個屬性是MemberOf,它基本上是給定帳戶所屬的組的列表。另外請注意Steen的答案是更新的方式來執行這種類型的事情,所以如果你正在運行.NET 4.0或更高版本,它可能會更容易走這條路線。 –

+0

如果我將searchresult發送到標籤,則返回: LDAP://domain.company.org/CN=smith \,john,OU =普通用戶帳戶,OU = Users,OU = TTH,OU =中央區域,DC = domain,DC = company,DC = org 感覺就像我很親密,但我無法弄清楚! –