未设置活动目录搜索获取对象引用

本文关键字:搜索 获取 对象引用 设置 活动 | 更新日期: 2023-09-27 18:01:41

下面的代码通过从Active Directory返回用户的全名在我的本地机器上工作:

string principal = System.Web.HttpContext.Current.Request.LogonUserIdentity.Name.Remove(0, 12);
string filter = string.Format("(&(ObjectClass={0})(sAMAccountName={1}))", "person", principal);
string[] properties = new string[] { "fullname" };
DirectoryEntry adRoot = new DirectoryEntry("LDAP://myserver.com");
adRoot.AuthenticationType = AuthenticationTypes.Secure;
DirectorySearcher searcher = new DirectorySearcher(adRoot);
searcher.SearchScope = SearchScope.Subtree;
searcher.ReferralChasing = ReferralChasingOption.All;
searcher.PropertiesToLoad.AddRange(properties);
searcher.Filter = filter;
SearchResult result = searcher.FindOne();
DirectoryEntry directoryEntry = result.GetDirectoryEntry();
string displayName = directoryEntry.Properties["displayName"][0].ToString();
if (string.IsNullOrEmpty(displayName) == false)
{
    return displayName;
}

当我将其发布到开发服务器时,我得到以下错误:

系统。NullReferenceException:对象引用没有设置为实例

在以下行抛出错误:

DirectoryEntry directoryEntry = result.GetDirectoryEntry();

我试过了

DirectoryEntry adRoot = new DirectoryEntry("LDAP://" + domain, AdAdminUsername, AdAdminPassword, AuthenticationTypes.Secure);

但仍然没有快乐。

任何想法?

谢谢!

未设置活动目录搜索获取对象引用

在当前代码中,必须在调用FindOne()后检查NULL -如果没有找到匹配的目录条目,它可以返回NULL值。

见MSDN文档:

如果在搜索过程中找到多个条目,则只找到第一个返回条目。如果没有找到与搜索匹配的条目

返回一个空引用(在Visual Basic中为空)。

另外,在访问之前,您还应该始终检查是否存在属性-它可能不存在.....

因此,你的代码应该是这样的:
SearchResult result = searcher.FindOne();
if(result != null)
{
    DirectoryEntry directoryEntry = result.GetDirectoryEntry();
    if(directoryEntry.Properties["displayName"] != null &&
       directoryEntry.Properties["displayName"].Length > 0)
    {
       string displayName = directoryEntry.Properties["displayName"][0].ToString();
       if (!string.IsNullOrEmpty(displayName))
       {
          return displayName;
       }
    }
}

但是:如果你使用的是。net 3.5或更新版本,你应该看看System.DirectoryServices.AccountManagement命名空间,它在处理活动目录时使很多事情变得容易得多。

您可以使用PrincipalSearcher和"按例查询"主体来进行搜索:

// create your domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
// define a "query-by-example" principal - here, we search for a UserPrincipal 
// and with the first name (GivenName) of "Bruce"
UserPrincipal qbeUser = new UserPrincipal(ctx);
qbeUser.SamAccountName = "whatever you're looking for.....";
// create your principal searcher passing in the QBE principal    
PrincipalSearcher srch = new PrincipalSearcher(qbeUser);
// find all matches
foreach(var found in srch.FindAll())
{
    // do whatever here - "found" is of type "Principal" - it could be user, group, computer.....          
}

如果你还没有-绝对阅读MSDN文章管理目录安全主体在.NET框架3.5中,很好地展示了如何充分利用System.DirectoryServices.AccountManagement的新特性

当然,根据您的需要,您可能希望在您创建的"按示例查询"用户主体上指定其他属性:

  • Surname(或姓)
  • DisplayName(通常:名+空格+姓)
  • SAM Account Name -您的Windows/AD帐户名
  • User Principal Name -你的"username@yourcompany.com"样式名

您可以在UserPrincipal上指定任何属性,并将其用作PrincipalSearcher的"按例查询"。

只是从外部:您的字符串称为principal是由以下构建的:

string principal = System.Web.HttpContext.Current.Request.LogonUserIdentity.Name.Remove(0, 12);

您是否在开发服务器上记录了'System.Web.HttpContext.Current.Request.LogonUserIdentity.Name ' ?这个静态构造可能是麻烦的开始,因为如果principal不是你想的那样,FindOne的结果可能是NULL。