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

时间:2009-10-05 10:21:31

标签: asp.net active-directory windows-server-2008

在Windows 2000高级服务器上有Active目录,我在Windows 2008服务器企业版上有一个Web服务器,以下代码在Winsows 2003服务器上工作正常,但是当我安装Win 2008服务器时,它给了我以下错误,webserver不是AD服务器的子域。但它们具有相同的范围IP地址。

  

发生了本地错误。

     

System.DirectoryServices.DirectoryServicesCOMException

我想从我的网络服务器验证通过AD,我甚至测试端口389并且它是开放的(通过telnet),我甚至将端口389 UDP和TCP添加到网络服务器的防火墙以确保它是开放的,即使我转过身防火墙关闭但没有改变。我不知道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 个答案:

答案 0 :(得分:4)

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

我编写了一个快速功能,您可以尝试哪个功能应该比您当前使用的代码更好:

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;
  }
}

但是,我建议您在您的域中设置一个服务帐户,并在您的应用程序中使用该帐户(使用您知道的密码),而不是使用您所在的用户的用户名/密码连接到该目录autenticating。

答案 1 :(得分:1)

DirectorySearcher类很可能是罪魁祸首。

DirectorySearcher上的每个MSDN:

“Windows 7,Windows Vista SP1或更高版本,Windows XP SP3, Windows XP SP2 x64 Edition,Windows Server 2008(不支持服务器核心角色), Windows Server 2008 R2(服务器)不支持核心角色),Windows Server 2003 SP2

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

答案 2 :(得分: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。但是,如果用户属于所提供的组,则不会进行任何检查。