检查DirectoryEntry在DirectorySearcher中是否有效 [英] Check if the DirectoryEntry is valid in DirectorySearcher

查看:116
本文介绍了检查DirectoryEntry在DirectorySearcher中是否有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

随着我们的网络最近扩展,我正在尝试在新域和新域控制器上搜索AD.我在下面指定的域是Web服务器加入的域.我将其称为domainA,它可以正常工作.当我将其更改为domainB时,它似乎总是从domainA返回结果.我可以在DirectorySeracher()中放入新的域条目或什至是"blahblahblah"之类的任何字符串,它会从DomainA返回结果.如果找不到我指定的域,Web服务器会以某种方式退回到该域吗?我没有任何错误,只是来自错误域的结果.

I am trying to search the AD on a new domain and new domain controller as our network has recently expanded. The domain I specify below is the domain the web server is joined to. I will refer to this as domainA and it works correctly. When I change it to domainB, it appears to always return results from domainA. I can put the new domain entry or even any string like "blahblahblah" inside DirectorySeracher() and it returns results from DomainA. Is it falling back to the domain the web server is joined to somehow if it can't find the domain I specify? I don't get any errors, just results from the wrong domain.

      DirectorySearcher dssearch = new DirectorySearcher("LDAP://CN=users,DC=LAZARUS,DC=COM");
      dssearch.Filter = "(&(objectClass=user)(sAMAccountName=" + txtusername.Text + "))";
      SearchResult sresult = dssearch.FindOne();
      if ( sresult != null ){
          lblStatus.Visible = false;    
           DirectoryEntry dsresult = sresult.GetDirectoryEntry();   
           lblfname.Text = dsresult.Properties["givenName"][0].ToString();
           lbllname.Text = dsresult.Properties["sn"][0].ToString();
           lblTitle.Text = dsresult.Properties["description"][0].ToString();
           lblHire.Text = dsresult.Properties["whencreated"][0].ToString();
           pnlForm.Visible = false;
           pnlResults.Visible = true;
           btnReset.Visible = true;
    }else{
           lblStatus.Visible = true;
           lblStatus.Text = "User not found.";
    }

推荐答案

您使用的DirectorySearcher(string)构造函数实际上期望使用过滤器,而不是搜索根路径.

The constructor you used DirectorySearcher(string) is actually expecting the filter, but not the search root path.

DirectorySearcher dssearch = new DirectorySearcher("LDAP://CN=users,DC=LAZARUS,DC=COM");

在第二行中,您将覆盖过滤器的值

And in the 2nd line you overwrite the value of the filter

dssearch.Filter = "(&(objectClass=user)(sAMAccountName=" + txtusername.Text + "))";

因此,传递给ctor的任何内容都完全无效.

So anything you passed to the ctor has no effect at all.

DirectorySearcher的搜索根必须作为DirectoryEntry传递. 您可以在以下链接中选择最合适的ctor.

Search root for DirectorySearcher must be passed as DirectoryEntry. You may pick the most appropriate ctor in the following link.

http://msdn.microsoft.com/zh-CN/library/system.directoryservices.directorysearcher%28v=vs.110%29.aspx

这篇关于检查DirectoryEntry在DirectorySearcher中是否有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆