Authenticating Domain Users with System.DirectoryServices

后端 未结 2 464
盖世英雄少女心
盖世英雄少女心 2021-01-02 19:58

Given a username and a password for a domain user, what would be the best way to authenticate that user programatically?

2条回答
  •  清酒与你
    2021-01-02 20:34

    It appears that .NET 3.5 added a new namespace to deal with this issue - System.DirectoryServices.AccountManagement. Code sample is below:

    Private Function ValidateExternalUser(ByVal username As String, ByVal password As String) As Boolean
        Using context As PrincipalContext = New PrincipalContext(ContextType.Domain, _defaultDomain)
            Return context.ValidateCredentials(username, password, ContextOptions.Negotiate)
        End Using
    End Function
    

    The namespace also seems to provide a lot of methods for manipulating a domain account (changing passwords, expiring passwords, etc).

提交回复
热议问题