/
ForgotPassword.aspx.cs
246 lines (204 loc) · 8.22 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Security.Cryptography;
using System.Text;
using System.Net;
using MySql.Data.MySqlClient;
public partial class ForgotPassword : System.Web.UI.Page
{
DBOperations dbOps;
Logger log;
Links links;
General general;
int sessionTimeoutMinutes = int.Parse(ConfigurationManager.AppSettings["sessionTimeoutMinutes"]);
int isDoPasswordHashing = int.Parse(ConfigurationManager.AppSettings["isDoPasswordHashing"]);
string stoockerMail = ConfigurationManager.AppSettings["stoockerMail"];
private string smtpClient = ConfigurationManager.AppSettings["smtpClient"];
static Label ErrorLabel;
protected void Page_Load(object sender, EventArgs e)
{
dbOps = (DBOperations)Application["dbOps"];
log = (Logger)Application["log"];
links = (Links)Application["links"];
general = (General)Application["general"];
}
protected void SubmitButton_Click(object sender, EventArgs e)
{
ErrorLabel = (Label)PasswordRecovery1.UserNameTemplateContainer.FindControl("ErrorLabel");
string username = PasswordRecovery1.UserName.Trim();
string message = "";
if (username.Length >= 4)
{
string userMailID = GetUserMailID(username);
if (userMailID != "-1" && userMailID != "")
{
// Reset the Password.
string newPassword = GenerateRandomPassword();
// EMail the New Password to the User.
string subject = "Message from Stoocker.com";
string userMessage = "Your Password has been reset. The new Password is: " + newPassword;
bool isMailed = general.SendMail(stoockerMail, userMailID, subject, userMessage);
if (isMailed)
{
// Hash the Password
string passwordHash = newPassword;
if (isDoPasswordHashing == 1)
{
passwordHash = dbOps.HashPassword(newPassword);
}
// Reset the Password in the Database.
ResetPassword(username, passwordHash);
message = "Your Password has been E-Mailed.";
}
else
{
message = "An error occurred while E-Mailing the Password";
}
}
else
{
message = "No such Username exists for Stoocker.com";
}
}
else
{
message = "All UserID's are atleast 4 characters long.";
}
ErrorLabel.Text = message;
}
protected void PasswordRecovery1_SendingMail(object sender, MailMessageEventArgs e)
{
//ErrorLabel = (Label)PasswordRecovery1.FindControl("ErrorLabel");
//string username = PasswordRecovery1.UserName.Trim();
//string message = "";
//if (username.Length >= 4)
//{
// string userMailID = GetUserMailID(username);
// if (userMailID != "-1" && userMailID != "")
// {
// // Reset the Password.
// string newPassword = GenerateRandomPassword();
// // EMail the New Password to the User.
// string subject = "Message from Stoocks";
// string userMessage = "Your Password has been reset. The new Password is: " + newPassword;
// SendMail("vatsals@vatsals.com", userMailID, subject, userMessage);
// // Hash the Password
// string passwordHash = HashPassword(newPassword);
// // Reset the Password in the Database.
// ResetPassword(username, passwordHash);
// message = "Your Password has been E-Mailed.";
// }
// else
// {
// message = "No such Username exists for Stoocks.";
// }
// ErrorLabel.Text = message;
//}
}
private string GetUserMailID(string username)
{
string eMail = "";
string queryString = @"SELECT EMail FROM stoocks.registration WHERE Username='" + username + "' ;";
MySqlDataReader retList = dbOps.ExecuteReader(queryString);
if(retList != null && retList.HasRows)
while (retList.Read())
{
eMail = retList.GetString(0);
}
retList.Close();
return eMail;
}
private string GenerateRandomPassword()
{
StringBuilder randomPassword = new StringBuilder();
Random random = new Random();
char ch;
int size = random.Next(4, 12);
for (int i = 0; i < size; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
randomPassword.Append(ch);
}
return randomPassword.ToString();
}
private int ResetPassword(string username, string passwordHash)
{
// DBOperations dbOps = new DBOperations();
// Update the Registration Table;
string queryString = @"UPDATE stoocks.registration SET Password = '" + passwordHash + "' WHERE Username='" + username + "';";
int retVal = dbOps.ExecuteNonQuery(queryString);
if (retVal == 1)
{
// Update the Login Table;
queryString = @"UPDATE stoocks.login SET Password = '" + passwordHash + "' WHERE Username='" + username + "';"; ;
retVal = dbOps.ExecuteNonQuery(queryString);
if (retVal == 1)
{
}
else
{
retVal = -1;
}
}
else // An error occurred.
{
retVal = -1;
}
return retVal;
}
#region Login Methods (Not In Use Since December 2007)
/*
protected void LoginButton_Click(object sender, EventArgs e)
{
string stoocksUsername = Login1.UserName.Trim();
string stoocksPassword = Login1.Password.Trim();
string passwordHash = stoocksPassword;
// Generate the Hashed Password if PasswordHashing Parameter from Web.Config is turned ON.
if (isDoPasswordHashing == 1)
{
passwordHash = dbOps.HashPassword(stoocksPassword);
}
bool isValid = IsValidCredentials(stoocksUsername, passwordHash);
if (isValid)
{
CreateSession(stoocksUsername);
Response.Redirect(links.HomePageLink);
}
else
{
Login1.FailureText = "Please Try Again.";
}
}
private bool IsValidCredentials(string username, string password)
{
bool isValid = false;
string queryString = @"SELECT Count(*) FROM stoocks.LOGIN WHERE Username='" + username + "' AND Password='" + password + "' ;";
int retVal = dbOps.ExecuteScalar(queryString);
if (retVal >= 1)
{
isValid = true;
}
return isValid;
}
private void CreateSession(string username)
{
//FormsAuthentication.Initialize();
////The AddMinutes determines how long the user will be logged in after leaving the site if he doesn't log off.
//FormsAuthenticationTicket fat = new FormsAuthenticationTicket(1, username, DateTime.Now, DateTime.Now.AddMinutes(30), false, username);
//Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(fat)));
//Response.Redirect(FormsAuthentication.GetRedirectUrl(username, false));
// Session.Timeout = 30;
Session.Timeout = sessionTimeoutMinutes;
Session.Add("username", username);
}
*/
#endregion Login Methods (Not In Use Since December 2007)
}