Click here to Skip to main content
15,918,343 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Net.Mail;
using System.Net;
using System.IO;
public partial class ForgotPassword : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)


    protected void btnResetPass_Click(object sender, EventArgs e)
        using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["KalzAgency"].ConnectionString))
            SqlCommand cmd = new SqlCommand("Select * from tblUsers where Email=@Email", con);
            cmd.Parameters.AddWithValue("@Email", txtEmailID.Text);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            if (dt.Rows.Count != 0)
                String myGUID = Guid.NewGuid().ToString();
                int Uid = Convert.ToInt32(dt.Rows[0][0]);

                SqlCommand cmd1 = new SqlCommand("Insert into ForgotPass(Id,Uid,RequestDateTime) values('" + myGUID + "','" + Uid + "',GETDATE())", con);

                //Send Reset link via Email

                String ToEmailAddress = dt.Rows[0][3].ToString();
                String Username = dt.Rows[0][1].ToString();
                String EmailBody = "Hi ," + Username + ",<br/><br/>Click the link below to reset your password<br/> <br/> http://localhost:1288/RecoverPassword.aspx?id=" + myGUID;

                MailMessage PassRecMail = new MailMessage("", ToEmailAddress);

                PassRecMail.Body = EmailBody;
                PassRecMail.IsBodyHtml = true;
                PassRecMail.Subject = "Reset Password";

                using (SmtpClient client = new SmtpClient())
                    client.EnableSsl = true;
                    client.UseDefaultCredentials = false;
                    client.Credentials = new NetworkCredential("", "1234");
                    client.Host = "";
                    client.Port = 587;
                    client.DeliveryMethod = SmtpDeliveryMethod.Network;



                lblResetPassMsg.Text = "Reset Link send ! Check Your email for reset password";
                lblResetPassMsg.ForeColor = System.Drawing.Color.Green;
                txtEmailID.Text = string.Empty;
                lblResetPassMsg.Text = "Oops! This Email Does not Exist...Try agian ";
                lblResetPassMsg.ForeColor = System.Drawing.Color.Red;
                txtEmailID.Text = string.Empty;



What I have tried:

I have tried turn on less secure app after sign in google account.
Richard Deeming 14-Apr-21 4:55am    
SqlCommand cmd1 = new SqlCommand("Insert into ForgotPass(Id,Uid,RequestDateTime) values('" + myGUID + "','" + Uid + "',GETDATE())", con);

Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]

You already know how to use parameters - you've done it for the previous query. So why opt for the insecure version here?
Richard Deeming 14-Apr-21 4:55am    
Also note that a link to http://localhost:1288 won't work once you've deployed your application. It only works for you when you are debugging the site in Visual Studio.
Member 15100384 15-Apr-21 3:33am    
Is there other option besides using that link?
Richard Deeming 15-Apr-21 4:42am    
You need to use the proper public link to your application. Eg:
Richard Deeming 14-Apr-21 4:57am    
And you'll probably want to rate-limit the emails to a single user. Otherwise, an attacker could submit thousands of "forgotten password" requests for the same user every second, which lead to your app flooding the user's mailbox, and probably being blocked for spam.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900