ASP.NET change AD password

0

I know this is ugly and I know I should have it in try blocks I've just been tinkering trying to get it to work. I'm missing something here and I would really appreciate some help figuring it out. All I'm trying to do is to create a page with a text box for the login name as well as one for the old password and two for the new password. To add more information the error I'm getting now is Unknown name. (Exception from HRESULT: 0x80020006 (DISP_E_UNKNOWNNAME)) when i try to invoke the ChangePassword

     Dim userid As String
    Dim password As String
    Dim login As String
    Dim check As String
    login = txtLogin.Text
    userid = txtLogin.Text
    password = txtOldPass.Text
    Dim entry As DirectoryEntry = New DirectoryEntry("LDAP://OU=PaidUsers,DC=LOCAL,DC=bb", "LOCAL\" & userid, password)

    Dim obj As Object = entry.NativeObject
    Dim search As DirectorySearcher = New DirectorySearcher(entry)
    search.SearchScope = SearchScope.Subtree
    search.Filter = "(SAMAccountName=" & login & ")"
    Dim result As SearchResult = search.FindOne()

    check = CType(result.Properties("sAMAccountName")(0), String)

    If check = login Then
        If txtNewPass.Text = txtNewPass2.Text Then
            entry = result.GetDirectoryEntry()
            entry.Username = "LOCAL\" & check
            entry.Password = txtOldPass.Text
            entry.AuthenticationType = AuthenticationTypes.Secure
            entry.Options.Referral = ReferralChasingOption.All
            entry.Invoke("ChangePassword", txtOldPass.Text, txtNewPass.Text)
            entry.CommitChanges()
            lblSuccess.Visible = True

        Else
            lblPasswdError.Visible = True
        End If

    Else
        lblError.Visible = True
    End If
asp.net
vb.net
active-directory
asked on Stack Overflow May 12, 2014 by JTuman • edited May 13, 2014 by JTuman

1 Answer

0

The error that fixed it was the command to Get the directory entry before Invoking the "ChangePassword". What I didn't realize is once I go into the IF logic the entry directory entry didn't have the account I was trying to change, hence the UnknownName error. Calling the result of my initial query of LDAP found the user account and it worked like a charm. The working code is below;

    lblError.Visible = False
    lblSuccess.Visible = False
    lblPasswdError.Visible = False
    lblCatch.Visible = False
    Dim userid As String
    Dim password As String
    Dim check As String
    userid = txtLogin.Text
    password = txtOldPass.Text
    Dim entry As DirectoryEntry = New DirectoryEntry("LDAP://OU=PaidUsers,DC=LOCAL,DC=bb", "LOCAL\" & userid, password)

    Try

        Dim search As DirectorySearcher = New DirectorySearcher(entry)
        search.SearchScope = SearchScope.Subtree
        search.Filter = "(SAMAccountName=" & userid & ")"
        Dim result As SearchResult = search.FindOne()

        check = CType(result.Properties("sAMAccountName")(0), String)
        If check <> Nothing Then

            If check = userid Then
                If txtNewPass.Text = txtNewPass2.Text Then
                    entry = result.GetDirectoryEntry()
                    entry.Username = "LOCAL\" & check
                    entry.Password = txtOldPass.Text
                    entry.AuthenticationType = AuthenticationTypes.Secure
                    entry.Options.Referral = ReferralChasingOption.All
                    entry.Invoke("ChangePassword", txtOldPass.Text, txtNewPass.Text)
                    entry.CommitChanges()
                    lblSuccess.Visible = True

                Else
                    lblPasswdError.Visible = True
                End If

            Else
                lblError.Visible = True
            End If
        Else
            lblError.Visible = True
        End If
    Catch ex As Exception
        lblCatch.Text = "Error message: " + ex.InnerException.Message
        lblCatch.Visible = True
    End Try
answered on Stack Overflow May 16, 2014 by JTuman

User contributions licensed under CC BY-SA 3.0