在Windows 2008服务器中连接AD时发生了本地错误。

4
在Windows 2000高级服务器上有Active Directory,在Windows 2008企业版服务器上有一个Web服务器,以下代码在Windows 2003服务器上运行良好,但当我安装Win 2008服务器时,它给我以下错误,Web服务器不是AD服务器的子域,但它们具有相同的IP地址范围。
“出现本地错误。 System.DirectoryServices.DirectoryServicesCOMException”
我想通过AD从我的Web服务器进行身份验证,我甚至测试了端口389,并且它是开放的(通过telnet),我甚至将端口389 UDP和TCP添加到Web服务器的防火墙中以确保它是开放的,甚至关闭了防火墙,但仍然无法运行我的代码。我不知道Windows 2008服务器出了什么问题,找遍互联网也没有找到解决方案。任何解决方案都会有所帮助。谢谢。
public bool IsAuthenticated(string username, string  pwd,string group)
{
  string domainAndUsername = "LDAP://192.xx.xx.xx:389/DC=test,DC=oc,DC=com" ;
  string usr="CN=" + username + ",CN=" + group;
  DirectoryEntry entry = new DirectoryEntry(domainAndUsername, usr, pwd,
                                            AuthenticationTypes.Secure );

  try
  {
    DirectorySearcher search = new DirectorySearcher(entry);

    search.Filter = "(SAMAccountName=" + username + ")";

    SearchResult result = search.FindOne();

    if (result == null)
    {
        return false;
    }
  }
  catch (Exception ex)
  {
      return false;
  }
  return true;
}
3个回答

4

好的,让我们尝试一种不同的方法...您表示您正在使用Windows 2008,这意味着您应该能够使用.NET 3.5中引入的新System.DirectoryServices.AccountManagement命名空间。

我编写了一个快速函数,您可以尝试一下,它应该比您当前使用的代码更好:

using System.DirectoryServices.AccountManagement;

//...

private Boolean IsAuthenticated(String username, String password, String group)
{
  PrincipalContext domain;
  try
  {
    // Connect to the domain:
    domain = new PrincipalContext(ContextType.Domain, "192.xx.xx.xx", username, password);
  }
  catch
  {
    // Unable to connect to the domain (connection error or bad username/password):
    return false;
  }

  PrincipalSearcher searcher = new PrincipalSearcher();

  // Search for the user in the domain:
  UserPrincipal findUser = new UserPrincipal(domain);
  findUser.SamAccountName = username;
  searcher.QueryFilter = findUser;
  UserPrincipal foundUser = (UserPrincipal)searcher.FindOne();

  // Search for the group in the domain:
  GroupPrincipal findGroup = new GroupPrincipal(domain);
  findGroup.SamAccountName = group;
  searcher.QueryFilter = findGroup;
  GroupPrincipal foundGroup = (GroupPrincipal)searcher.FindOne();

  if (foundGroup != null)
  {
    // Return true if group exists and the user is a member:
    return foundUser.IsMemberOf(foundGroup);
  }
  else
  {
    // Group was not found:
    return false;
  }
}

然而,我建议您在域中设置一个服务账户,并在应用程序中使用该账户(带有您知道的密码),而不是使用正在验证的用户的用户名/密码连接目录。


1

DirectorySearcher类很可能是罪魁祸首。

根据MSDN上的DirectorySearcher:

"Windows 7、Windows Vista SP1或更高版本、Windows XP SP3、Windows XP SP2 x64版、Windows Server 2008(不支持Server Core角色)、Windows Server 2008 R2(不支持Server Core角色)、Windows Server 2003 SP2。

.NET Framework不支持所有平台的所有版本。有关受支持版本的列表,请参阅.NET Framework系统要求。"


0

你收到的错误提示表明你可以访问Active Directory(不是防火墙问题),但AD无法处理请求。

我不确定为什么这段代码在Server 2003上能够工作,因为这两行代码...

string usr="CN=" + username + ",CN=" + group;
DirectoryEntry entry = new DirectoryEntry(domainAndUsername, usr, pwd,  AuthenticationTypes.Secure );  

...不应该工作,因为您没有以正确的方式提供用户名(您不能简单地将用户名添加到组名中,它不是有效的 DN)。如果更改为...

DirectoryEntry entry = new DirectoryEntry(domainAndUsername, username, pwd,  AuthenticationTypes.Secure );

...你应该能够成功连接到AD。但是,不会检查用户是否属于提供的组。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接