Set password for active directory lightweight directory services (ad lds) on .net 2.0

0

I am trying to create a new user and set their password in AD LDS using asp.net vb. I'm binding to an instance of a directory entry, which is working fine. And I can add a user without a problem. The problem is that I can't seem to set the password when I add the user. Is this the right way to set the password?

Dim objADAM As DirectoryEntry = BindToInstance()

Dim objUser As DirectoryEntry = objADAM.Children.Add("CN=Jimmy", "User")
objUser.Properties("sn").Value = "lloyd"
objUser.Properties("givenName").Value = "Jimmy Smith"
objUser.Properties("userpassword").Value = "THEPASSWORD"
objUser.CommitChanges()

This is the error that I get :

System.DirectoryServices.DirectoryServicesCOMException (0x80072020): An operations error occurred. (Exception from HRESULT: 0x80072020) at System.DirectoryServices.DirectoryEntry.CommitChanges()

I've also tried this :

Dim objADAM As DirectoryEntry = BindToInstance()

Dim objUser As DirectoryEntry = objADAM.Children.Add("CN=Jimmy", "User")
objUser.Properties("sn").Value = "lloyd"
objUser.Properties("givenName").Value = "Jimmy Smith"
objUser.CommitChanges()
objUser.Invoke("SetPassword", New Object() {"123456789A$#"})
objUser.CommitChanges()

Which gave me this error :

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Runtime.InteropServices.COMException (0x8000500D): The directory property cannot be found in the cache. --- End of inner exception stack trace --- at System.DirectoryServices.DirectoryEntry.Invoke(String methodName, Object[] args)

asp.net
vb.net
active-directory
asked on Stack Overflow Mar 10, 2011 by FarFigNewton • edited Mar 24, 2012 by Joel Coehoorn

1 Answer

2

My coworker found a solution. You call CreateUserSetPassword to create the user and setup the password in one function call.

FYI, if the set password fails, the user will already be set up, so you'll need to either delete the user or just call the SetPassword function again.

Class variables

   Private Uri As String
    ' { get; set; }
    Private OuUri As String
    ' { get; set;}
    Private UserUri As String
    ' { get; set; }
    'You will want to set these two parameters somewhere in .config and pass to
    'or otherwise make available to this process
    Private userid As String = "danny123"
    Private pwd As String = "pa$$word1"

New function

Public Sub New(ByVal uri__1 As String, ByVal ou As String)
    Uri = uri__1
    OuUri = "LDAP://" & uri__1 & "/" & ou
    UserUri = "LDAP://" & uri__1 & "/CN={0}," & ou
End Sub

CreateUserSetPassword

''' <summary>
''' Creates new user and sets password
''' </summary>
''' <param name="userName"></param>
''' <param name="password"></param>
Public Function CreateUserSetPassword(ByVal userName As String, ByVal password As String) As String
    Dim oGUID As String = String.Empty
    oGUID = CreateUserAccount(userName, password)
    If oGUID = String.Empty Then
        oGUID = SetPassword(userName, password)
        If oGUID = String.Empty Then
            oGUID = EnableUser(userName)
        End If
    End If
    Return oGUID
End Function

CreateUserAccount

''' <summary>
''' Create user in the AD-LDS
''' </summary>
''' <param name="userName"></param>
''' <param name="userPassword"></param>
''' <returns></returns>
Public Function CreateUserAccount(ByVal userName As String, ByVal userPassword As String) As String
    Dim oGUID As String = String.Empty
    Try
        Dim connectionPrefix As String = OuUri
        Using dirEntry As New DirectoryEntry(connectionPrefix, userid, pwd)
            Dim newUser As DirectoryEntry = dirEntry.Children.Add("CN=" & userName, "user")
            newUser.Properties("userPrincipalName").Value = userName
            newUser.CommitChanges()
            newUser.Close()

        End Using
        'catch (System.DirectoryServices.DirectoryServicesCOMException E)
    Catch E As Exception
        oGUID = E.Message
    End Try
    Return oGUID
End Function

SetPassword

''' <summary>
''' Set password for the user in AD-LDS
''' </summary>
''' <param name="user"></param>
''' <param name="password"></param>
Public Function SetPassword(ByVal user As String, ByVal password As String) As String
    Dim oGUID As String = String.Empty
    Const adsOptionPasswordPortnumber As Long = 6
    Const adsOptionPasswordMethod As Long = 7
    Const adsPasswordEncodeClear As Integer = 1

    Const intPort As Integer = 50000
    Dim objUser As DirectoryEntry
    ' User object.
    ' Set authentication flags.
    Dim AuthTypes As AuthenticationTypes = AuthenticationTypes.Signing Or AuthenticationTypes.Sealing Or AuthenticationTypes.Secure

    ' Bind to user object using LDAP port.
    Try
        objUser = New DirectoryEntry(String.Format(UserUri, user), userid, pwd, AuthTypes)
        'Get object using GetDirectoryEntry
        'objUser = GetDirectoryEntry(user);
        objUser.RefreshCache()

        objUser.Invoke("SetOption", New Object() {adsOptionPasswordPortnumber, intPort})
        objUser.Invoke("SetOption", New Object() {adsOptionPasswordMethod, adsPasswordEncodeClear})
        objUser.Invoke("SetPassword", New Object() {password})
        objUser.CommitChanges()
    Catch e As Exception
        oGUID = e.Message & vbLf & vbCr & Convert.ToString(e.InnerException)
    End Try
    Return oGUID
End Function
answered on Stack Overflow Mar 14, 2011 by FarFigNewton

User contributions licensed under CC BY-SA 3.0