2013-03-24 97 views
3

最近我將ASP.NET應用程序從運行IIS5的舊服務器移動到運行IIS7.5的新服務器。Asp.net從IIS搜索Active Directory

應用程序給我一個錯誤:

The (&(objectCategory=person)(sAMAccountName=)) search filter is invalid.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentException: The (&(objectCategory=person)(sAMAccountName=)) search filter is invalid.

,搜索AD功能是:

public static string Get_AD_User_Email(string username) 
{ 
     try 
     { 
      DirectorySearcher searcher = new DirectorySearcher("(&(objectCategory=person)(sAMAccountName=" + username + "))"); 
      SearchResult result = searcher.FindOne(); 

      if (result != null) 
      { 
       DirectoryEntry employee = result.GetDirectoryEntry(); 

       if (employee.Properties["mail"] != null) 
       { 
        return employee.Properties["mail"].Value.ToString(); 
       } 
       else return "NULL"; 
      } 
      else throw new Exception("ERROR: Problem searching active directory for users."); 
     } 
     catch (Exception ex) { throw ex; } 
    } 

奇怪的是,在Visual Studio中調試的網站上運行,只能從IIS它是崩潰。

有人可以幫助我嗎?

回答

-1

嘗試: 對象類=用戶

相反的: objectCategory =人

2

麻煩的就是你的函數Get_AD_User_Email(string username)被稱爲與username空值。

1

由於:

DirectorySearcher searcher = new DirectorySearcher("(&(objectCategory=person)(sAMAccountName=" + username + "))"); 

還是返回了異常:

The (&(objectCategory=person)(sAMAccountName=)) search filter is invalid.

你傳遞一個空字符串Get_AD_User_Email

你如何檢索「用戶名」?

1

您更改了IIS服務器,現在沒有用戶名被調用方法傳入(如其他幾個答案指出的那樣)。

我將驗證您在IIS中的該網站上禁用了匿名訪問。通常找到啓用Windows身份驗證和匿名訪問。發生這種情況時,匿名是首選,你不會得到用戶名。

檢查HttpContext.Current.User的值。我通常使用下面的代碼來驗證Windows身份驗證:

WindowsIdentity id = (WindowsIdentity)HttpContext.Current.User.Identity; 
string username = id.Name;