2009-10-05 45 views
4

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

发生了本地错误。

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

回答

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

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

+0

非常感谢你,它对我来说工作正常 – Sara 2009-10-13 05:13:03

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连接成功。但是,如果用户属于提供的组,则不会有任何检查。

1

DirectorySearcher类很可能是罪魁祸首。

每MSDN上的DirectorySearcher:

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

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

相关问题