Active Directory - Check username / password

后端 未结 4 768
有刺的猬
有刺的猬 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:14

    If you're using .net 3.5 use this code instead.

    To authenticate a user:

    PrincipalContext adContext = new PrincipalContext(ContextType.Domain);
    
    using (adContext)
    {
         return adContext.ValidateCredentials(UserName, Password);
    }
    

    If you need to find the user to R/W attributes to the object do this:

    PrincipalContext context = new PrincipalContext(ContextType.Domain);
    UserPrincipal foundUser = 
        UserPrincipal.FindByIdentity(context, "jdoe");
    

    This is using the System.DirectoryServices.AccountManagement namespace so you'll need to add it to your using statements.

    If you need to convert a UserPrincipal object to a DirectoryEntry object to work with legacy code you can do this:

    DirectoryEntry userDE = (DirectoryEntry)foundUser.GetUnderlyingObject();
    

提交回复
热议问题