Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Professional
|
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 14th, 2007, 11:19 AM
Friend of Wrox
 
Join Date: Aug 2006
Posts: 131
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to kherrerab Send a message via Yahoo to kherrerab
Default how to use MembershipProvider.EncryptPassword

I want to change the pwd of a user that has forget it. I could change the pwd in the aspnet_membership but i want to keep it encrypted and not in plain text.

thanks

 
Old March 14th, 2007, 12:18 PM
Friend of Wrox
 
Join Date: Aug 2006
Posts: 131
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to kherrerab Send a message via Yahoo to kherrerab
Default

Note: This method is new in the .NET Framework version 2.0.

Resets a user's password to a new, automatically generated password.

Namespace: System.Web.Security
Assembly: System.Web (in system.web.dll)

Syntax
Visual Basic (Declaration)
Public Overridable Function ResetPassword ( _
    passwordAnswer As String _
) As String

Visual Basic (Usage)
Dim instance As MembershipUser
Dim passwordAnswer As String
Dim returnValue As String

returnValue = instance.ResetPassword(passwordAnswer)

C#
public virtual string ResetPassword (
    string passwordAnswer
)

C++
public:
virtual String^ ResetPassword (
    String^ passwordAnswer
)

J#
public String ResetPassword (
    String passwordAnswer
)

JScript
public function ResetPassword (
    passwordAnswer : String
) : String



Parameters
passwordAnswer
The password answer for the membership user.



Return Value
The new password for the membership user.
Remarks
ResetPassword calls the MembershipProvider.ResetPassword method of the membership provider referenced by the ProviderName property to reset the password for the membership user to a new, automatically generated password. The new password is then returned to the caller.

If EnablePasswordReset is false, the membership provider will return an exception.

If RequiresQuestionAndAnswer is false, you can supply a null reference (Nothing in Visual Basic) for the answer parameter, or use the ResetPassword overload that does not take any parameters.

If a password answer is required and an incorrect password answer is supplied, a MembershipPasswordException is thrown by the membership provider.

Example
The following code example resets a user's password and returns the new, automatically generated password.

Visual Basic Copy Code
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Web.Security" %>
<script runat="server">

Dim u As MembershipUser

Public Sub Page_Load(sender As Object, args As EventArgs)
  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."

      QuestionLabel.Text = ""
      QuestionLabel.Enabled = False
      AnswerTextBox.Enabled = False
      ResetPasswordButton.Enabled = False
    Else
      QuestionLabel.Text = u.PasswordQuestion
      QuestionLabel.Enabled = True
      AnswerTextBox.Enabled = True
      ResetPasswordButton.Enabled = True
    End If
End Sub

Public Sub ResetPassword_OnClick(sender As Object, args As EventArgs)
  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(AnswerTextBox.Text)
  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: " & Server.HtmlEncode(newPassword)
  Else
    Msg.Text = "Password reset failed. Please re-enter your values and try again."
  End If
End Sub


</script>
<html>
<head>
<title>Sample: Reset Password</title>
</head>
<body>

<form runat="server">
  <h3>Retrieve Password</h3>

  <asp:Label id="Msg" runat="server" ForeColor="maroon" /><BR>

  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>

  Password Question: <B><asp:Label id="QuestionLabel" runat="server" /></B><BR>

  Answer: <asp:TextBox id="AnswerTextBox" Columns="60" runat="server" Enabled="False" />
          <asp:RequiredFieldValidator id="AnswerRequiredValidator" runat="server"
                                      ControlToValidate="AnswerTextBox" ForeColor="red"
                                      Display="Static" ErrorMessage="Required" Enabled="False" /><BR>

  <asp:Button id="ResetPasswordButton" Text="Reset Password"
              OnClick="ResetPassword_OnClick" runat="server" Enabled="False" />

</form>

</body>
</html>









Similar Threads
Thread Thread Starter Forum Replies Last Post
failed to login with membershipprovider swifty_programmer ASP.NET 2.0 Professional 3 May 4th, 2007 02:53 AM
failed to login with membershipprovider swifty_programmer ASP.NET 2.0 Basics 1 May 3rd, 2007 11:40 AM
Error when loading custom MembershipProvider mega ASP.NET 2.0 Professional 18 November 8th, 2006 04:47 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.