Win32: How to validate credentials against Active Directory?

前端 未结 5 679
一个人的身影
一个人的身影 2020-11-30 01:08

It has been asked, and answered for .NET, but now it\'s time to get an answer for native Win32 code:

How do i validate a Windows username and password?

5条回答
  •  情深已故
    2020-11-30 02:00

    I authenticated user, by username & password like this :

    username is user sn attribute value in Ldap server, like U12345

    userDN is user DistinguishedName in LdapServer

    public bool AuthenticateUser(string username, string password)
    {
    try
    {
    var ldapServerNameAndPort = "Servername:389";
    var userDN = string.Format("CN=0},OU=Users,OU=MyOU,DC=MyDC,DC=com",username);
    var conn = new LdapConnection(ldapServerNameAndPort)
    {
     AuthType = AuthType.Basic
    };
    conn.Bind(new NetworkCredential(userDN , password));
    return true;
    }
    catch (Exception e)
    {
     return false;
    }
    

    }

提交回复
热议问题