Password Recovery In Asp.Net

Password Recovery In Asp.Net

In this post we will learn how to do Password Recovery In Asp.Net. Here we are recovering password through code, we are not using membership. Earlier we had learnt about How To Block The UserName After 3 Invalid Password Attempts. Here our approach is to check if user is registered then we will sent him a password to his email id.


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="">
<head runat="server">
<title>Password Recovery In Asp.Net, Password Recovery In Asp.Net without Using ASP.Net Membership</title>
<style type="text/css">
 width: 980px;
 margin: 0px auto;
 text-align: center;
 padding-top: 50px;
 font-size: 20px;
<form id="form1" runat="server">
<h2>Password Recovery In Asp.Net</h2>

<asp:TextBox ID="iemail" runat="server"></asp:TextBox> &nbsp;&nbsp;<asp:Button ID="send" runat="server" OnClick="send_Click" Text="Forgot Password" />

<br /><br />
<br /><br />
All rights reserved by <a href=""></a> | Hosting partner <a href="" target="_blank">Grootstech</a>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Net.Security;
using System.Net.Mail;

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

    protected void send_Click(object sender, EventArgs e)
        SqlConnection con= new SqlConnection("Data Source=.;Initial Catalog=dev;User ID=sa;Password=hightechnology");
        String stremail = iemail.Text;
        String replaceemail = stremail.Replace("'","''");
        String searchpassword = "";
        searchpassword = "select username from users where uemail='" + replaceemail.ToString() + "'";
        String password;
        string strValue;
           SqlCommand cmd = new SqlCommand(searchpassword, con);
           strValue = Convert.ToString(cmd.ExecuteScalar());
        catch (Exception ex)
            throw ex;
        password = strValue.ToString();
        if (password.ToString() != "")
            MailMessage mail = new MailMessage();
            mail.From = new MailAddress("", "Hightechnology-Technology That Teach You");
            mail.IsBodyHtml = true;
            mail.Subject = "User Forgot Password Login Details";
            mail.Body = "<table border='0' cellpadding='0' width='100%'>" +
                   "<tr><td width='70%'><p>" +
                   "<table border='0' cellpadding='0' width='100%'><tr>" +
                   "<td colspan='2'><p><strong>User Password Recovery Details</strong></p></td></tr>" +
                   "<tr><td colspan='2'>&nbsp;</td></tr>" +
                   "<tr><td  align='left'>Username:</td><td ' align='left'>" + iemail.Text + "</td>" +
                   "</tr>" +
                   "<tr><td  align='left'>Password:</td>" +
                    "<td  align='left'>" + password.ToString() + "</td>" +
                    "</tr>" +
                    "<tr><td colspan='2'>&nbsp;</td></tr>" +
                   "<tr><td colspan='2'><p>Please login by navigating to <a href='' target='_blank'></a></p></td></tr>" +
                    "</table></td>" +
                    "<td width='30%'></td></tr>" +
                    "<tr><td><br /><br/></td><td></td></tr>" +
                    "<tr><td><p>Thank you & Great Regards,</p></td><td></td></tr>" +
                    "<tr><td><p>Team Hightechnology</p></td><td></td></tr>" +
            SmtpClient smtp = new SmtpClient("", 587);
            smtp.Credentials = new System.Net.NetworkCredential("", "qazwsx@123");
            smtp.EnableSsl = true;
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "StartUpScript2", "alert('The password has been sent to your registered email ID')", true);
                    iemail.Text = "";
                catch (Exception)
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "StartUpScript2", "alert('Error While Sending Mail, Please Try Again')", true);
                ScriptManager.RegisterStartupScript(this, this.GetType(), "StartUpScript2", "alert('Please check. This ID is not registered with us.')", true);
                iemail.Text = "";