/
register.aspx.cs
253 lines (219 loc) · 9.75 KB
/
register.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
247
248
249
250
251
252
253
using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.UI;
using Npgsql;
using NpgsqlTypes;
namespace FooBlog
{
public partial class register : Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack) return;
RequestToken.Value = FooSessionHelper.SetToken(HttpContext.Current);
}
protected void submitButton_Click(object sender, EventArgs e)
{
string alias = aliasText.Text;
string email = emailText.Text;
string address = addressText.Text;
string city = cityText.Text;
string country = countryText.Text;
string username = usernameText.Text;
string pass = passText.Text;
if (!String.IsNullOrEmpty(alias) && FooStringHelper.IsValidEmailAddress(email) &&
!String.IsNullOrEmpty(address) &&
!String.IsNullOrEmpty(city) && !String.IsNullOrEmpty(country) && !String.IsNullOrEmpty(username) &&
!String.IsNullOrEmpty(pass))
{
if (FooSessionHelper.IsValidRequest(HttpContext.Current, RequestToken.Value))
{
string userId = FooStringHelper.RandomString(16);
if (!CheckIfUsernameExists(username) && !FooEmailHelper.CheckIfEmailExists(email, username))
{
errorPanel.Visible = false;
formPanel.Visible = false;
successPanel.Visible = true;
string defaultGroup = ConfigurationManager.AppSettings["User Group ID"] ?? "ri3EKpc5Z5gN4FEu";
bool insertedUser = RegisterNewUser(userId, alias, email, address, city, country, username, pass,
defaultGroup);
successLabel.Text = insertedUser
? "Your account has been successfully created. You can proceed to <a href=\"login.aspx\">log on</a>."
: "Failed to create account. The administrator has been notified. Please try again.";
errorPanel.Visible = false;
errorLabel.Text = "";
}
else
{
errorPanel.Visible = true;
errorLabel.Text = "Some details already exist in this application.";
}
}
else
{
errorPanel.Visible = true;
errorLabel.Text = "Invalid request.";
}
}
else
{
errorPanel.Visible = true;
errorLabel.Text = "Incomplete or invalid details.";
}
RequestToken.Value = FooSessionHelper.SetToken(HttpContext.Current);
}
public static bool CheckIfUsernameExists(string username)
{
try
{
using (var conn = new NpgsqlConnection())
{
// App-DB connection.
conn.ConnectionString =
ConfigurationManager.ConnectionStrings["fooPostgreSQL"].ConnectionString;
conn.Open();
var cmd = new NpgsqlCommand
{
CommandText =
"SELECT username FROM users WHERE username= @USERNAME",
CommandType = CommandType.Text,
Connection = conn
};
var nameParam = new NpgsqlParameter
{
ParameterName = "@USERNAME",
NpgsqlDbType = NpgsqlDbType.Varchar,
Size = 32,
Direction = ParameterDirection.Input,
Value = username
};
cmd.Parameters.Add(nameParam);
NpgsqlDataReader dr = cmd.ExecuteReader();
string result = String.Empty;
while (dr.Read())
{
result = dr["username"].ToString();
}
dr.Close();
return !String.IsNullOrEmpty(result);
}
}
catch (Exception ex)
{
FooLogging.WriteLog(ex.ToString());
return false;
}
}
public static bool RegisterNewUser(string id, string alias, string email, string address, string city,
string country,
string username, string pass, string groupId)
{
try
{
using (var conn = new NpgsqlConnection())
{
// App-DB connection.
conn.ConnectionString =
ConfigurationManager.ConnectionStrings["fooPostgreSQL"].ConnectionString;
conn.Open();
var cmd = new NpgsqlCommand
{
CommandText =
"INSERT INTO Users (userId, userName, userAlias, passwordHash, groupId, email, address, city, country, profileimg) VALUES (@USERID, @USERNAME, @USERALIAS, @PASSWORDHASH, @GROUPID, @EMAIL, @ADDRESS, @CITY, @COUNTRY, 'profile_default.jpg');",
CommandType = CommandType.Text,
Connection = conn
};
var idParam = new NpgsqlParameter
{
ParameterName = "@USERID",
NpgsqlDbType = NpgsqlDbType.Varchar,
Size = 16,
Direction = ParameterDirection.Input,
Value = id
};
cmd.Parameters.Add(idParam);
var nameParam = new NpgsqlParameter
{
ParameterName = "@USERNAME",
NpgsqlDbType = NpgsqlDbType.Varchar,
Size = 32,
Direction = ParameterDirection.Input,
Value = username
};
cmd.Parameters.Add(nameParam);
var aliasParam = new NpgsqlParameter
{
ParameterName = "@USERALIAS",
NpgsqlDbType = NpgsqlDbType.Varchar,
Size = 32,
Direction = ParameterDirection.Input,
Value = alias
};
cmd.Parameters.Add(aliasParam);
var hashParam = new NpgsqlParameter
{
ParameterName = "@PASSWORDHASH",
NpgsqlDbType = NpgsqlDbType.Varchar,
Direction = ParameterDirection.Input,
Value = FooCryptHelper.CreateShaHash(pass)
};
cmd.Parameters.Add(hashParam);
var groupParam = new NpgsqlParameter
{
ParameterName = "@GROUPID",
NpgsqlDbType = NpgsqlDbType.Varchar,
Direction = ParameterDirection.Input,
Value = groupId
};
cmd.Parameters.Add(groupParam);
var emailParam = new NpgsqlParameter
{
ParameterName = "@EMAIL",
NpgsqlDbType = NpgsqlDbType.Varchar,
Size = 64,
Direction = ParameterDirection.Input,
Value = email
};
cmd.Parameters.Add(emailParam);
var addressParam = new NpgsqlParameter
{
ParameterName = "@ADDRESS",
NpgsqlDbType = NpgsqlDbType.Varchar,
Size = 128,
Direction = ParameterDirection.Input,
Value = address
};
cmd.Parameters.Add(addressParam);
var cityParam = new NpgsqlParameter
{
ParameterName = "@CITY",
NpgsqlDbType = NpgsqlDbType.Varchar,
Size = 32,
Direction = ParameterDirection.Input,
Value = city
};
cmd.Parameters.Add(cityParam);
var countryParam = new NpgsqlParameter
{
ParameterName = "@COUNTRY",
NpgsqlDbType = NpgsqlDbType.Varchar,
Size = 32,
Direction = ParameterDirection.Input,
Value = country
};
cmd.Parameters.Add(countryParam);
cmd.ExecuteNonQuery();
cmd.Dispose();
}
return true;
}
catch (Exception ex)
{
FooLogging.WriteLog(ex.ToString());
return false;
}
}
}
}