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 感觉就像我很亲密,但我无法弄清楚! –