ASP.NET更改AD密码

时间:2014-05-12 01:23:27

标签: asp.net vb.net active-directory

我知道这很难看,而且我知道我应该尝试使用它。我只是想修补它试图让它发挥作用。我在这里遗漏了一些东西,我真的很感激帮助搞清楚。我要做的就是创建一个页面,其中包含登录名的文本框以及旧密码的一个页面和新密码的两个页面。要添加更多信息,我现在得到的错误是 名字不明。 (来自HRESULT的异常:0x80020006(DISP_E_UNKNOWNNAME)) 当我尝试调用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

1 个答案:

答案 0 :(得分:0)

修复它的错误是在调用“ChangePassword”之前获取目录条目的命令。我没有意识到的是,一旦我进入IF逻辑,入口目录条目没有我试图更改的帐户,因此UnknownName错误。调用我的LDAP初始查询结果找到了用户帐户,它就像一个魅力。工作代码如下;

    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