Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4066502
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T16:08:15+00:00 2026-05-20T16:08:15+00:00

I am trying to create a new user and set their password in AD

  • 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)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T16:08:15+00:00Added an answer on May 20, 2026 at 4:08 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a new instance of Excel using VBA using: Set
I'm trying to create a new user on my development active directory server using
In a view I'm trying to create a new user and then log them
Im trying to test my successfully creates a new user after login (using authlogic).
I'm trying to create a new Excel file using jxl, but am having a
I am trying to create a new contact using Dynamic Entity. The sample i
I’m trying to create a script for a user to enter in their username,
I'm trying to create new user in WebLogic 11g security realm. InitialContext ctx =
I am using the built-in register module in asp.net application for registering new users.
I am trying to create a new user object with a specific Role. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.