2009-10-05 44 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系統要求。「

相關問題