在.NET上创建LDAP连接

unhi4e5o  于 2023-02-10  发布在  .NET
关注(0)|答案(3)|浏览(187)

我正在尝试使用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)

我需要一些建议,我错过了什么。任何资源将是有帮助的

kyxcudwk

kyxcudwk1#

我有一个有点类似的问题,这个服务器和谷歌送我到这里。
我看到的一个问题是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
}
vngu2lb8

vngu2lb82#

尝试使用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");
llew8vvj

llew8vvj3#

命名空间-使用系统目录服务协议;
方法

private bool ldapValidateUser(string fullname, string password)
        {
 
try
            {
                LdapDirectoryIdentifier ldap = new LdapDirectoryIdentifier("Directory Host", true, false); 
                LdapConnection connection = new LdapConnection(ldap);
                connection.AuthType = AuthType.Basic;

                string ldapuser = "cn=" + fullname + ",ou=Org Unit,dc=Value,dc=local";
                connection.Credential = new System.Net.NetworkCredential(ldapuser, password);
                connection.Bind();
                
                return true;
            }

            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
     return false;
        }

相关问题