Categories
Asp, Asp.net

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 asp.net 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.

Design:

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

<!DOCTYPE html>

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

<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="http://www.hightechnology.in">www.Hightechnology.in</a> | Hosting partner <a href="http://www.grootstech.com" target="_blank">Grootstech</a>
</div>
</form>
</body>
</html>

Code:

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;
        con.Open();
        string strValue;
        try
        {
           SqlCommand cmd = new SqlCommand(searchpassword, con);
           strValue = Convert.ToString(cmd.ExecuteScalar());
        }
        catch (Exception ex)
        {
            throw ex;
        }
        password = strValue.ToString();
        con.Close();
        if (password.ToString() != "")
        {
            MailMessage mail = new MailMessage();
            mail.From = new MailAddress("hightechnology@gmail.com", "Hightechnology-Technology That Teach You");
            mail.To.Add(iemail.Text);
            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='http://www.hightechnology.in' target='_blank'>www.hightechnology.in</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>" +
                    "</table>";
            SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
            smtp.Credentials = new System.Net.NetworkCredential("hightechnology@gmail.com", "qazwsx@123");
            smtp.EnableSsl = true;
                try
                {
                    smtp.Send(mail);
                    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);
                }
                }
            else
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "StartUpScript2", "alert('Please check. This ID is not registered with us.')", true);
                iemail.Text = "";
            }

        }
    }