-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForgotPassword.aspx.cs
69 lines (61 loc) · 2.57 KB
/
ForgotPassword.aspx.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Net.Mail;
using System.Net;
public partial class ForgotPassword : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btPassRec_Click(object sender, EventArgs e)
{
String CS = ConfigurationManager.ConnectionStrings["MyDatabaseConnectionString1"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
SqlCommand cmd = new SqlCommand("select * from users where Email='"+tbEmailId.Text+"'",con);
con.Open();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
if (dt.Rows.Count != 0)
{
String myGUID = Guid.NewGuid().ToString();
int Uid = Convert.ToInt32(dt.Rows[0][0]);
SqlCommand cmd1 = new SqlCommand("insert into ForgotPassRequests values('"+myGUID+"','"+Uid+"',getdate())",con);
cmd1.ExecuteNonQuery();
//send 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:48599/RecoverPassword.aspx?Uid="+myGUID;
MailMessage PassRecMail = new MailMessage("[email protected]", ToEmailAddress);
PassRecMail.Body = EmailBody;
PassRecMail.IsBodyHtml = true;
PassRecMail.Subject = "Reset Password";
//SmtpClient SMTP = new SmtpClient("smtp-mail.outlook.com", 587);
SmtpClient SMTP = new SmtpClient("smtp.gmail.com", 587);
SMTP.Credentials = new NetworkCredential()
{
UserName = "[email protected]",
Password = "youremailpassword"
};
SMTP.EnableSsl = true;
SMTP.Send(PassRecMail);
lblPassRec.Text = "Check your email to reset your password.";
lblPassRec.ForeColor = Color.Green;
}
else
{
lblPassRec.Text = "OOps This email id DOES NOT exist in our database !";
lblPassRec.ForeColor = Color.Red;
}
}
}
}