locked
Unable to SetPassword from usr.Invoke under DirectoryEntry in RRS feed

  • Question

  • User-569283357 posted

    I am getting "InvalidCastException was unhandled" exception after usr.Invoke("SetPassword", New Object() {txtPwd.ToString}).  The detail exception is Unable to cast object of type 'System.Reflection.TargetInvocationException' to type 'System.Runtime.InteropServices.COMException'.

    I was able to use DirectoryEntry method to create a new user account but unable to set its initial password. 

    Please advise.

    Below is a portion of my code for your reference.

    Try
    Dim usr As DirectoryEntry = ent.Children.Add("CN=" + user, "user")

    usr.Properties("samAccountName").Value = txtLogin.Text
    usr.Properties("sn").Value = txtLastName.Text
    usr.Properties("displayname").Value = user
    usr.Properties("name").Value = user
    usr.Properties("userPrincipalName").Value = txtLogin.Text & "@" & cbDomain.Text
    usr.Properties("givenName").Value = txtFirstName.Text
    usr.Properties("postofficebox").Value = txtFileNumber.Text
    usr.Properties("Division").Value = txtPayGroup.Text
    usr.Properties("employeenumber").Value = txtFileNumber.Text
    usr.CommitChanges()
    usr.Properties("pwdLastSet").Value = -1
    usr.Properties("userAccountControl").Value = usr.Properties("userAccountControl").Value And Not (ADS_UF_ACCOUNTDISABLE + ADS_UF_PASSWD_CANT_CHANGE + ADS_UF_DONT_EXPIRE_PASSWD)
    usr.CommitChanges()
    usr.Invoke("SetPassword", New Object() {txtPwd.ToString})    <------ Exception Error Throw Here ------>
    usr.CommitChanges()
    lstUser.Items.Add(user & " Added into Active Directory.")

    Catch except As Exception

    Dim COMEx As System.Runtime.InteropServices.COMException = CType(except, System.Runtime.InteropServices.COMException)
    lstUser.Items.Add(COMEx.ErrorCode & ". " & COMEx.Message)

    End Try

    Friday, November 25, 2005 11:59 PM

All replies

  • User2085602130 posted
    Hi fellow!


    Try this, I hope this helps you:

    usr.Properties("userAccountControl").Value = usr.Properties("userAccountControl").Value And Not (ADS_UF_ACCOUNTDISABLE + ADS_UF_PASSWD_CANT_CHANGE + ADS_UF_DONT_EXPIRE_PASSWD)
    usr.Invoke("SetPassword", New Object() {txtPwd.Text.Trim()}) 

    usr.CommitChanges()

    lstUser.Items.Add(user & " Added into Active Directory.")

    Catch except As Exception

    Dim COMEx As System.Runtime.InteropServices.COMException = CType(except, System.Runtime.InteropServices.COMException)
    lstUser.Items.Add(COMEx.ErrorCode & ". " & COMEx.Message)

    End Try

    You was trying to pass a TextBox to password, not the value of it!

    Greetings,
    Agus

    Saturday, November 26, 2005 5:25 AM
  • User-569283357 posted
    Thanks for the fast response. I am still getting the same error with usr.Invoke("SetPassword", New Object() {txtPwd.Text.Trim()}). I hard code the command with usr.Invoke("SetPassword", "Problem123") and getting the same error. It seems like we need to convert it to an array string. I even tried usr.Invoke("SetPassword", New Object() {txtPwd.Text.ToString}) with the same error. It points to review the convertible type error at below link: http://msdn.microsoft.com/en-us/library/7w0ksafs.aspx Any Suggestion? Brian
    Saturday, November 26, 2005 12:27 PM
  • User1354132231 posted
    Thanks for the fast response. I am still getting the same error with usr.Invoke("SetPassword", New Object() {txtPwd.Text.Trim()}). I hard code the command with usr.Invoke("SetPassword", "Problem123") and getting the same error. It seems like we need to convert it to an array string. I even tried usr.Invoke("SetPassword", New Object() {txtPwd.Text.ToString}) with the same error. It points to review the convertible type error at below link: http://msdn.microsoft.com/en-us/library/7w0ksafs.aspx Any Suggestion? Brian


    SetPassword is notoriously difficult to get working correctly.  Please read the sticky post called 'Common System.DirectoryServices Issues and Solutions'.  There is a link there to using SetPassword that should tell you everything you need to know.  Additionally, searching on the term 'SetPassword' should yield a number of results.

    Saturday, November 26, 2005 2:35 PM
  • User2085602130 posted
    Hi folk!

    try this, it works for me!

    Dim result As Object = newUser.Invoke ("SetPassword", "NoProblem")
    newUser.CommitChanges()

    Greetings,
    Agus
    Sunday, November 27, 2005 3:27 PM
  • User1354132231 posted


    Hi folk!

    try this, it works for me!

    Dim result As Object = newUser.Invoke ("SetPassword", "NoProblem")
    newUser.CommitChanges()




    Two points:

    1.  You are relying on VB.NET to convert the last parameter to an array of object (object[]).  This should barf in most .NET languages, but apparently VB.NET is doing something special for you behind the scenes.  I would imagine you are not using Option Explicit.  Don't try this with another language.
    2.  I seriously doubt that the syntax of using this particular ADSI call is the issue.  *Sigh* If only things were so simple...



    Sunday, November 27, 2005 11:32 PM
  • User-569283357 posted

    Hi Agus and Ryan,

    Thank you for your suggestion.

    I am NOT getting access denied but I am still getting InvalidCastException was unhandled exception.  I may have casting problem instead.  What is your recommendation?  I am spending the whole weekend trying to figure out what went wrong with no luck.       

    Ryan had pointed me to the directions that he had replied to the others.  It educated me a lot more compare to yesterday.  I don't know if they apply to me while I am using VB.NET to write this code.  There is no web.config to allow me to configure Impersonate option.  I am not running my code on any ASP.NET web site in my Visual Studio 2005 project.  I am simply using VB.NET to test the DirectoryEntry module then convert it to ASP.NET once I am fully understand the complexity of this module.  I have not even get to the IUSR_MACHINE and impersonate issue that Ryan mentioned from his previous posting yet.  My AD domain server is running on W2003.  It is interesting that I can create user account but cannot set initial password for the user.  I have seen many people running into the same problem with ASP.NET but I have yet seen anyone run into the problem from VB.NET like I do.  What type of object does the system expecting since it complaints about unable to cast object type.  Could this be a bug in DirectoryEntry or I am missing something in the casting? 

    Thanks again!

    Brian

    Monday, November 28, 2005 12:05 AM
  • User1354132231 posted
    I believe I see your problem.  You are casting the resulting exception to a COMException without using the 'as' keyword to make sure it actually is a COMException.  For v2.0, they have updated the error to a DirectoryServicesException (or something like that), so the cast would probably fail.

    Still, the main issue is that your call to SetPassword is failing.  That you are getting a casting exception is incidental to the implementation.  If you simply output the exception to the Console, you might get a better idea.

    try
    {
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }


    Notice, I am just using the ToString() from the exception instance to get the entire stack trace.  Alternatively, you can put a break on the catch in the debugger and just view what the error is before the failed cast.

    Now, back to SetPassword - you will have to ensure that whatever account is running this code is:

    a.) an administrator in the domain since SetPassword requires this (or the necessary permissions)
    b.) running from a machine in the same domain as where the password is being set.  Without SSL, there is an additional burden to getting SetPassword to work in a machine from another domain.  It is possible, but the stars must be aligned correctly.


    Monday, November 28, 2005 1:21 PM
  • User-569283357 posted
    Thanks for both of you.  I got it working!

    Ryan,
    You are right.  I was having access denied condition where my domain administrator enabled me to create user account but did not enable me to set password.  Once they set my login account to Administrators, I am able to set password for new user.  In addition, I am getting more display on the exception as you stated. 

    Agus,
    Your Setpassword works for me too.

    Thanks agian and I really appreciated your help.  I would not be able to obtain these help from any technical books. 

    Brian
    Tuesday, November 29, 2005 1:33 AM
  • User638478306 posted

    here is the solution in code : 

      string newPassword = Membership.GeneratePassword(12, 4);
      string quotePwd;
      byte[] pwdBin;

      quotePwd = String.Format(@"""{0}""", newPassword);

      pwdBin = System.Text.Encoding.Unicode.GetBytes(quotePwd);

      UserEntry.Properties["unicodePwd"].Value = pwdBin;

      UserEntry.CommitChanges();

    ;) Mohamed Hachem

    Wednesday, July 21, 2010 9:32 AM