2013-03-24 103 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;