Thursday, November 10, 2011

Asp.net forgot Password-Send recovery link to email

protected void btnSubmit_Click(object sender, EventArgs e)
    {       
   
        DataSet ds_sendmail = new DataSet();
        SqlDataAdapter da_sendmail = new SqlDataAdapter("select * from  LOGIN_USERDETAILS where EmailAddress='" + txtEmailAddress.Text.Trim() + "'", con);
        da_sendmail.Fill(ds_sendmail);
        if (ds_sendmail.Tables[0].Rows.Count > 0)
        {
            string Username = ds_sendmail.Tables[0].Rows[0]["Username"].ToString();
            int Uid = int.Parse(ds_sendmail.Tables[0].Rows[0]["Uid"].ToString());
            string Email=ds_sendmail.Tables[0].Rows[0]["EmailAddress"].ToString();
            string resetdate=DateTime.Now.ToString();
          
            string Empname = ds_sendmail.Tables[0].Rows[0]["Firstname"].ToString() + " " + ds_sendmail.Tables[0].Rows[0]["Lastname"].ToString() + ",";
            string Title = "Password Recovery Link";

            if (Username != null)
            {
                 Guid guid;
                 guid = Guid.NewGuid();               
                 using (SqlCommand command = new SqlCommand())
                 {
                     command.Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnectionString"].ConnectionString);
                     command.CommandText = @"insert into [Password_Reset](Uid, Username, EmailAddress, Guid, Pwddate,active)
                        values(@Uid, @Username, @Email, @Guid, @Pwddate,@active)";
                     command.Parameters.Add("@Uid", SqlDbType.Int).Value =Uid;
                     command.Parameters.Add("@Username", SqlDbType.NVarChar, 50).Value = Username;
                     command.Parameters.Add("@Email", SqlDbType.NVarChar, 200).Value = Email;
                     command.Parameters.Add("@Guid", SqlDbType.UniqueIdentifier).Value = guid;
                     command.Parameters.Add("@Pwddate", SqlDbType.DateTime).Value = resetdate;
                     command.Parameters.Add("@active", SqlDbType.Int).Value = 1;
                     command.Connection.Open();
                     command.ExecuteNonQuery();
                 }
                 string pwdlink = "<a href='http:" + Request.Url.Authority.ToString() + "/AlphaStaff/PasswordReset.aspx?ResetID=" + guid + "'>Click this link to reset your Password</a>";
                
                ToEmailId = txtEmailAddress.Text.Trim();
                Description = "<h2>Password Reset :</h2> " + "\r\n";             
                Description += "<p>You are receiving this email because you filled out a form on alphastaff indicating that you had forgotten your password</p> " ; 
                Description += "<p>You can reset your Password using following link :</p> " + "\r\n";               
                Description += "<p>"+pwdlink+"</p>";
                Description += "<p>This link can be used for only once</p>";
                Description += "<p>This link will be expired after 24 hours</p>";
                Description += "<p>Thanks & Regards</p><p>Alphastaff Team</p>";
                Sendmail.Send(Empname,"sathish.sirikonda85@gmail.com", Title, Description, FromEmaiId);
                lblMsg.Visible = true;
                lblMsg.Text = "* Password reset link has sent to your Email address";
                txtEmailAddress.Text = "";
            }
        }
        else
        {
            lblMsg.Visible = true;
            lblMsg.Text = "* Given Emailaddress is Invalid !";
        }

    }

Password Reset Page  :


protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["ResetID"] != null)
        {
            ResetID = Request.QueryString["ResetID"].ToString();

            DataSet ds_sendmail = new DataSet();
            SqlDataAdapter da_sendmail = new SqlDataAdapter("select * from  Password_Reset where Guid='" + ResetID + "' and active=1", con);
            da_sendmail.Fill(ds_sendmail);
            if (ds_sendmail.Tables[0].Rows.Count > 0)
            {
                DateTime dTFrom;
                DateTime dTo;
                string sFrom = ds_sendmail.Tables[0].Rows[0]["Pwddate"].ToString();
                string sTo = DateTime.Now.ToString();
                if (DateTime.TryParse(sFrom, out dTFrom) && DateTime.TryParse(sTo, out dTo))
                {
                    TimeSpan TS = dTo.Subtract(dTFrom);                   
                    int diff_total_seconds = int.Parse(TS.TotalSeconds.ToString());

                    if (diff_total_seconds>86400)
                    {
                        Response.Redirect("~/LinkExpiry.aspx");
                    }

                }
                string Username = ds_sendmail.Tables[0].Rows[0]["Username"].ToString();
                Uid = ds_sendmail.Tables[0].Rows[0]["Uid"].ToString();
                string Email = ds_sendmail.Tables[0].Rows[0]["EmailAddress"].ToString();
                string resetdate = DateTime.Now.ToString();

            }
            else
            {
                Response.Redirect("~/LinkExpiry.aspx");
            }
        }
        else
        {
            Response.Redirect("~/LinkExpiry.aspx");
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
      
            ap.Action = 7;
            ap.Password = Common.BOL.Common_Details.Encrypt(txtNewpwd.Text);
            ap.Pwddate = Convert.ToDateTime(System.DateTime.Now.ToString("d"));
            ap.UserId =int.Parse(Uid);
            AllPages_frm.Add_Update_delete_AllPages_Pwd(ap);

            using (SqlCommand command = new SqlCommand())
            {
                command.Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnectionString"].ConnectionString);
                command.CommandText = @"update [Password_Reset] set active=@active where Guid='" + ResetID + "'";
                command.Parameters.Add("@Uid", SqlDbType.Int).Value = Uid;               
                command.Parameters.Add("@active", SqlDbType.Int).Value = 0;
                command.Connection.Open();
                command.ExecuteNonQuery();
            }

            lblErrmsg.Text = "* Password has been reset!";
            lblErrmsg.Visible = true;
            Response.Redirect("~/Login.aspx");
      
}

1 comment: