Thursday, 26 April 2012

Reset password In ASPNET membership VB

Reset password In ASPNET membership VB





ResetPassword.aspx

 <h3> Retrieve Password</h3>
    <p><asp:Label ID="Msg" runat="server" ForeColor="maroon" /><br />
    </p> Username:
    <asp:TextBox ID="UsernameTextBox" Columns="30" runat="server" AutoPostBack="True" />
    <asp:RequiredFieldValidator ID="UsernameRequiredValidator" runat="server" ControlToValidate="UsernameTextBox"
        ForeColor="red" Display="Static" ErrorMessage="Required" /><br />
    <asp:Button ID="ResetPasswordButton" Text="Reset Password" OnClick="ResetPassword_OnClick" runat="server" Enabled="False" />
    <br />
    <asp:Label ID="lblPassword" runat="server" ForeColor="maroon" Style="font-weight: 700;
        color: #FF0000; font-size: x-large" /><br />


ResetPassword.aspx.vb

Imports System.Web.Security

Public Class ResetPassword
    Inherits System.Web.UI.Page

    Dim u As MembershipUser

    Public Sub Page_Load(sender As Object, args As EventArgs) Handles Me.Load

        If Not Membership.EnablePasswordReset Then
            FormsAuthentication.RedirectToLoginPage()
        End If

        Msg.Text = ""

        If Not IsPostBack Then
            Msg.Text = "Please supply a username."
        Else
            VerifyUsername()
        End If
    End Sub

    Public Sub VerifyUsername()
        u = Membership.GetUser(UsernameTextBox.Text, False)

        If u Is Nothing Then
            Msg.Text = "Username " & Server.HtmlEncode(UsernameTextBox.Text) & " not found. Please check the value and re-enter."

            ResetPasswordButton.Enabled = False
        Else
            ResetPasswordButton.Enabled = True
        End If
    End Sub

    Protected Sub ResetPassword_OnClick(sender As Object, args As EventArgs) Handles ResetPasswordButton.Click
        Dim newPassword As String
        u = Membership.GetUser(UsernameTextBox.Text, False)

        If u Is Nothing Then
            Msg.Text = "Username " & Server.HtmlEncode(UsernameTextBox.Text) & " not found. Please check the value and re-enter."
            Return
        End If

        Try
            newPassword = u.ResetPassword()
        Catch e As MembershipPasswordException
            Msg.Text = "Invalid password answer. Please re-enter and try again."
            Return
        Catch e As Exception
            Msg.Text = e.Message
            Return
        End Try

        If Not newPassword Is Nothing Then
            Msg.Text = "Password reset. Your new password is: "
            lblPassword.Text = "" & Server.HtmlEncode(newPassword)
        Else
            Msg.Text = "Password reset failed. Please re-enter your values and try again."
        End If
    End Sub

End Class

0 comments:

Post a Comment