如何使用用户名和密码连接LDAP服务器?

时间:2017-03-15 06:33:10

标签: c# ldap

当我使用AuthenticationTypes.Anonymous连接到LDAP服务器时,它没问题:

var a = new DirectoryEntry("LDAP://localhost:389/dc=maxcrc,dc=com", "", "", AuthenticationTypes.Anonymous);

但是当我想使用用户名和密码连接服务器时:

var a = new DirectoryEntry("LDAP://localhost:389/dc=maxcrc,dc=com", "cn=Manager,dc=maxcrc,dc=com", "111111"); 

它会导致"指定无效的dn语法"错误:

enter image description here

如果我使用它:

var a = new DirectoryEntry("LDAP://localhost:389/dc=moe,dc=com", "cn=Manager,dc=moe,dc=com", "111111", AuthenticationTypes.Encryption);

它导致"服务器无法运行"错误:

enter image description here

那么如何使用用户名和密码连接到我的LDAP服务器?

3 个答案:

答案 0 :(得分:0)

验证用户名和密码的

使用此代码

foreach($_POST['times'] as $column){

    if(!in_array($column, array('someColumn', 'someOtherColumn', 'yetAnotherColumn')))
    {
        die("Column not permitted in query");
    }

    if(!($stmt = $mysqli->prepare("UPDATE stud_sched SET $column ='a' WHERE s_id = (?)"))) {
        echo "Prepare failed: "  . $stmt->errno . " " . $stmt->error;
    }

    if(!($stmt->bind_param("s",$pid))){
        echo "Bind failed: "  . $stmt->errno . " " . $stmt->error;
    }

    if(!$stmt->execute()){
        echo "Execute failed: "  . $stmt->errno . " " . $stmt->error;
    }

    $stmt->close();
}

并连接用户名和密码试试这个

using(PrincipalContext pc = new PrincipalContext(ContextType.Domain, "YOURDOMAIN"))
{
    // validate the credentials
    bool isValid = pc.ValidateCredentials("myuser", "mypassword");
}

答案 1 :(得分:0)

这是我连接到LDAP并验证用户身份时的一些代码。

 private DirectoryEntry dEntry = null;
 private DirectorySearcher dSearch = null;

 //Validate User Credentials in Active Directory
 dEntry = new DirectoryEntry(ADPath, userName, password, AuthenticationTypes.Secure);

 dSearch = new DirectorySearcher(dEntry);
 dSearch.PageSize = 1000;
 dSearch.PropertiesToLoad.Add("cn");
 if (dSearch.FindOne() != null)
 {
   //success
 }

答案 2 :(得分:0)

谢谢大家!我使用AuthenticationTypes.None

解决了这个问题

我的情况是我的ldap服务器在Windows上是openssh