【问题标题】:Creating LDAP Connection on .NET在 .NET 上创建 LDAP 连接
【发布时间】:2016-07-27 13:17:33
【问题描述】:

我正在尝试使用 c# 创建 LDAP 连接。

我找到了这个服务器,它提供了 LDAP 服务器进行测试

http://www.forumsys.com/en/tutorials/integration-how-to/ldap/online-ldap-test-server/

我在谷歌上搜索了很多帖子并试图创建一个统一的代码

  string domain = "ldap://ldap.forumsys.com/ou=mathematicians";
            string username = "cn=read-only-admin,dc=example,dc=com";
            string password = "password";
            string LdapPath = "Ldap://ldap.forumsys.com:389/ou=scientists,dc=example,dc=com";


            string domainAndUsername = domain + @"\" + username;
            DirectoryEntry entry = new DirectoryEntry(LdapPath, domainAndUsername, password);
            try
            {
                // Bind to the native AdsObject to force authentication.
                Object obj = entry.NativeObject;
                DirectorySearcher search = new DirectorySearcher(entry);
                search.Filter = "(SAMAccountName=" + username + ")";
                search.PropertiesToLoad.Add("cn");
                SearchResult result = search.FindOne();

                // Update the new path to the user in the directory
                LdapPath = result.Path;
                string _filterAttribute = (String)result.Properties["cn"][0];
            }
            catch (Exception ex)
            {

                throw new Exception("Error authenticating user." + ex.Message);
            }

此代码未连接它给出意外错误..

我还尝试了其他一些凭据,但它们也无济于事......

AUTH_LDAP_SERVER_URI = “ldap://ldap.forumsys.com”
AUTH_LDAP_BIND_DN = “cn=read-only-admin,dc=example,dc=com”
AUTH_LDAP_BIND_PASSWORD = “password”
AUTH_LDAP_USER_SEARCH = LDAPSearch(“ou=mathematicians,dc=example,dc=com”,
ldap.SCOPE_SUBTREE, “(uid=%(user)s)”)

--------------------
$config[‘LDAP’][‘server’] = ‘ldap://ldap.forumsys.com';
$config[‘LDAP’][‘port’] = ‘389’;
$config[‘LDAP’][‘user’] = ‘cn=read-only-admin,dc=example,dc=com';
$config[‘LDAP’][‘password’] = ‘password';

-------------------------
$config[‘LDAP’][‘server’] = ‘ldap://ldap.forumsys.com/ou=mathematicians';
$config[‘LDAP’][‘port’] = ‘389’;
$config[‘LDAP’][‘user’] = ‘gauss';
$config[‘LDAP’][‘password’] = ‘password';

--------------------------
OpenDSObject/GetObject functions, but don’t see a way to run a query with the ASDI objects.
Set LDAP = GetObject(“LDAP:”)
Set root = LDAP.OpenDSObject(“LDAP://ldap.forumsys.com:389″, “cn=read-only-admin,dc=example,dc=com”, “password”, 0)
Set ou = LDAP.OpenDSObject(“LDAP://ldap.forumsys.com:389/ou=mathematicians,dc=example,dc=com””, “cn=read-only-admin,dc=example,dc=com”, “password”, 0)
Set user = LDAP.OpenDSObject(“LDAP://ldap.forumsys.com:389/uid=riemann,dc=example,dc=com”, “cn=read-only-admin,dc=example,dc=com”, “password”, 0)

我需要一些我缺少的建议。任何资源都会有所帮助

【问题讨论】:

  • 什么意外错误?
  • 您是否尝试过提供正确的协议:LDAP 而不是 Ldap 或 ldap?根据我刚刚用谷歌搜索的forums.asp.net/t/…,这可能是导致问题的原因。

标签: c# .net active-directory


【解决方案1】:

我在这台服务器上遇到了一些类似的问题,谷歌把我发到这里。

我看到的一个问题是 LDAP 路径中区分大小写的问题。我们也应该指定 AuthenticationType。

请检查以下应该可以工作的代码块。

string ldapServer = "LDAP://ldap.forumsys.com:389/ou=scientists,dc=example,dc=com";
string userName = "cn=read-only-admin,dc=example,dc=com";
string password = "password";

var dirctoryEntry = new DirectoryEntry(ldapServer, userName, password, AuthenticationTypes.ServerBind);

try {
    object nativeObject = dirctoryEntry.NativeObject;
    //Rest of the logic
} catch (Exception ex) {
    //Handle error
}

【讨论】:

    【解决方案2】:

    尝试使用 PrincipalContext 连接到 LDAP 服务器。这是我开始时参考的一篇很好的操作指南文章:http://ianatkinson.net/computing/adcsharp.htm

    ctx = new PrincipalContext(
        ContextType.Domain,
        "contoso.local",
        "OU=Security Groups,OU=Contoso Inc,DC=contoso,DC=local",
        "contoso\sysadmin",
        "P@ssword1");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-25
      • 1970-01-01
      • 2011-12-16
      • 1970-01-01
      • 1970-01-01
      • 2023-03-18
      • 2019-11-20
      • 1970-01-01
      相关资源
      最近更新 更多