Wednesday, 22 May 2013

Reset and Email Forgotten Password Programmatically


Reset and Email Forgotten Password Programmatically


.aspx code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Reset and Email Forgotten Password.aspx.cs" Inherits="RandomNumber_Reset_and_Email_Forgotten_Password" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
    <style type="text/css">
#forgetPass
{
background-color:#F7F7DE;
border-color:#CCCC99;
font-size:10pt;
border-style:solid;
border-width:1px;
width:300px;
}
</style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <p>Reset and Email Forgotten Password Programmatically</p>
    <div>
<asp:Login ID="Login1" runat="server" BackColor="#F7F7DE" BorderColor="#CCCC99"
BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana"
Font-Size="10pt" DestinationPageUrl="~/Default.aspx">
<TitleTextStyle BackColor="#6B696B" Font-Bold="True" ForeColor="#FFFFFF" />
</asp:Login>
<br />
<div id="forgetPass">
Forgot Password?<br />
Username:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Go" />
<br />
<asp:Label ID="Label1" runat="server"></asp:Label>
</div>
</div>
    </div>
    </form>
</body>
</html>


.cs code

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net.Mail;


public partial class RandomNumber_Reset_and_Email_Forgotten_Password : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        // creating MembershipUser object from username supplied in textbox
        MembershipUser user = Membership.GetUser(TextBox1.Text);
        // resetting user's password and returning it in string to send in an email
        string newPassword = user.ResetPassword();
        // creating mailmessage object for new message
        MailMessage message = new MailMessage();
        // adding user's email in To list of email
        message.To.Add(user.Email);
        // creating subject for email
        message.Subject = "Your Username and Password";
        // setting message's body type to html
        message.IsBodyHtml = true;
        // preparing email body
        message.Body = @"Your pass is: " + newPassword;
        // creating smtpClient object
        SmtpClient smtp = new SmtpClient();
        // sending email
        smtp.Send(message);
        // updating label to show message that email has been sent
        Label1.Text = "Your new password has been sent to your registered email account!";
    }
}

No comments:

Post a Comment