C# 服务器无法运行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17820604/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:34:28  来源:igfitidea点击:

The server is not operational

c#active-directoryldap

提问by user2327795

This is the code I'm using to connecting to LDAP

这是我用来连接到 LDAP 的代码

 using (DirEntry = new DirectoryEntry(string.Format("LDAP://{0}/{1}", this.Host, ServerName)))
        {
            DirEntry.RefreshCache();
            if (!string.IsNullOrEmpty(UserName))
            {
                DirEntry.Username = UserName;
                DirEntry.Password = PassWord;
            }
            if (DirEntry.Properties.Contains("objectGUID"))
            {
                byte[] guiddatet = (byte[])DirEntry.Properties["objectGUID"].Value;
                return new Guid(guiddatet);
            }

I get "The server is not operational" error message when I run the code.

运行代码时收到“服务器无法运行”错误消息。

Can someone please tell me where I'm doing it wrong. And is there anyway to replace the above code with direct LDAP query.

有人可以告诉我我哪里做错了。无论如何用直接LDAP查询替换上面的代码。

采纳答案by X3074861X

You should try breaking this into separate parts, so it's easier to manage the logic, and easier to locate where your errors are occurring. I usually go with the following approach in this situation :

您应该尝试将其分解为单独的部分,以便更轻松地管理逻辑,并更轻松地定位发生错误的位置。在这种情况下,我通常采用以下方法:

  • Create an LdapConnectionobject so you can set the options you need
  • Setup a NetworkCredentialinstance with an administrative username and password
  • Bind to the directory with the user so you can issue a direct LDAP query
  • Return a SearchResultEntryso you can process the properties
  • 创建一个LdapConnection对象,以便您可以设置所需的选项
  • NetworkCredential使用管理用户名和密码设置实例
  • 与用户绑定到目录,以便您可以发出直接 LDAP 查询
  • 返回 aSearchResultEntry以便您可以处理属性

You have a few options to help you accomplish this, but I'd try something like this :

您有几个选项可以帮助您完成此操作,但我会尝试以下操作:

//Delcare your Network Credential with the administrative Username, Password, and your active directory domain
var credentials = new NetworkCredential(userName, password, domain);

//Create a directory identifier and connection, 
var ldapidentifier = new LdapDirectoryIdentifier(serverName, port, false, false);
var ldapconn = new LdapConnection(ldapidentifier, credentials);

Next, make sure you're setting the right AuthTypefor your particular instance. Since you're connecting over port 389, just use AuthType.Basic.

接下来,确保您AuthType为特定实例设置了权限。由于您通过端口 389 进行连接,因此只需使用AuthType.Basic.

ldapconn.AuthType = AuthType.Basic;

As you had asked, there is a very easy way to setup a direct LDAP query using this approach. I'm assuming you're searching by sAMAccountName, but you can modify this as needed :

正如您所问的,有一种非常简单的方法可以使用这种方法设置直接 LDAP 查询。我假设您正在搜索sAMAccountName,但您可以根据需要修改它:

string ldapFilter = "(&(objectCategory=person)(objectClass=user)(&(sAMAccountName={{UserYouAreTryingToFind}})))";

Now we just have to setup the search request, and send it accordingly :

现在我们只需要设置搜索请求,并相应地发送它:

//Send the search request with our delimited attribute list
var getUserRequest = new SearchRequest(domain, ldapFilter, SearchScope.Subtree, AttributeList)
                                     {SizeLimit = 1};

//Suppress any refferal creation from happening during the search
var SearchControl = new SearchOptionsControl(SearchOption.DomainScope);
getUserRequest.Controls.Add(SearchControl);
var userResponse = (SearchResponse)ldapconn.SendRequest(getUserRequest);

//This is where I load up the entry I've located, 
SearchResultEntry ResultEntry = userResponse.Entries[0];

That should return the user you've queried for, along with any properties you've put into AttributeList. In this context, AttributeListis just a string array (string[]) of property names - in your case you'll want to add one called "objectGUID".

这应该返回您查询的用户,以及您放入的任何属性AttributeList。在这种情况下,AttributeList它只是一个string[]属性名称的字符串数组 ( ) - 在您的情况下,您需要添加一个名为“objectGUID”的数组。

As for reading the properties on the SearchResultEntry, you can do exactly what you had originally :

至于阅读 上的属性SearchResultEntry,您可以完全按照您原来的方式进行操作:

 if(ResultEntry.Attributes.Contains("objectGUID"))
 {
     // do some stuff here
 }

That should help get you going in the right direction.

这应该有助于让你朝着正确的方向前进。

Also, if you don't already have a copy of wireshark, I highly suggest you download it - it will be invaluable in diagnosing connection issues with active directory.

此外,如果您还没有wireshark的副本,我强烈建议您下载它 - 它对于诊断与活动目录的连接问题非常有用。