tags:

views:

176

answers:

4

Trying to implement asp.net passwordrecovery control.

I want to catch newly generated password before sending to user and add some custom logic to it and then send the mail. there is only one event sendingmail event.

How can I catch it ?

+1  A: 

The ValidatingPassword event is raised when the CreateUser method, the ChangePassword method, or the ResetPassword method of a membership provider is called.

alex
Your quote should read "The ValidatingPassword event is raised DURING the [...] ResetPassword method". It continues "The ValidatingPassword event occurs before the action requested and enables the user to continue or cancel the requested action", so this occurs BEFORE the new password is generated.
Zhaph - Ben Duguid
The Password field of the event argument contains newly generated password.
alex
A: 

What "custom logic" do you want to implement?

If you want to modify the email that is sent, then hooking into the SendingMail event on the control is the way to go.

Use this event to do any special processing required before sending the e-mail message, such as setting MailMessage properties.

This will give you access to the MailDefinition object that enables you to supply different body copy etc. The MailDefinition documents list the substitutions that will occur as well (this can also be created declaratively):

<asp:PasswordRecovery ID="PasswordRecovery1" Runat="server" 
  SubmitButtonText="Get Password" SubmitButtonType="Link">
  <MailDefinition From="[email protected]" 
    Subject="Your new password"
    BodyFileName="PasswordMail.txt" />
</asp:PasswordRecovery>

The new password would be stored in the database at this stage, so you could modify it there in this event prior to sending the email - have you tried that?

Zhaph - Ben Duguid
+1  A: 

You could handle the SendingMail event, generate a new password using ResetPassword, and then update the body of the email with your new password and whatever else you wanted to put in there.

protected void PasswordRecovery1_SendingMail(object sender, MailMessageEventArgs e)
{
    // grab the user.
    MembershipUser mu = Membership.GetUser(PasswordRecovery1.UserName);

    if (mu != null)
    {

        // reset the password.
        string newPass = mu.ResetPassword();

        // switch out the body of the email.
        e.Message.Body = string.Format("New Password: {0}\n", newPass);

    }

}
Scott Ivey
Scott, thanks. I used the way you suggest. I used mu.ResetPassword(); for handling it.
Jack
A: 

Are you planning on storing the generated password somewhere, as the phrasing of your questions suggests you want to be able to read it and do something with it?

ck
ck, I want to store it in a database table for resending if needed. sometimes mail goes junk or not go to client. I need to resend the mail again
Jack
That potentially opens you up to a major security issue, passwords should never be stored in plain text.
ck