Active Directory - Check username / password

后端 未结 4 767
有刺的猬
有刺的猬 2020-12-04 08:39

I\'m using the following code on Windows Vista Ultimate SP1 to query our active directory server to check the user name and password of a user on a domain.

p         


        
4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-04 09:12

    I found that same code floating around the Internet on multiple websites and it didn't work for me. Steve Evans is probably right that if you're on .NET 3.5, you should not use this code. But if you ARE still on .NET 2.0 you can try this to Authenticate to your AD services:

    DirectoryEntry entry = new DirectoryEntry("LDAP://" + domain, 
       userName, password, 
       AuthenticationTypes.Secure | AuthenticationTypes.SecureSocketsLayer);
    object nativeObject = entry.NativeObject;
    

    The first line creates a DirectoryEntry object using domain, username, and password. It also sets the AuthenticationTypes. Notice how I'm setting both Secure (Kerberos) Authentication and SSL using the "Bitwise OR" ( '|' ) operator between the two parameters.

    The second line forces the NativeObject of "entry" to Bind to the AD services using the information from the first line.

    If an exception is thrown, then the credentials (or settings) were bad. If no exception, you're authenticated. The exception message will usually indicate what went wrong.

    This code is pretty similar to what you already have, but the domain is used where you have "path", and the username is not combined with the domain. Be sure to set your AuthenticationTypes properly, too. This can make or break the ability to authenticate.

提交回复
热议问题