Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Method for validating the e-mail
- public bool ValidateEmail(string emailaddress)
- {
- string email = emailaddress;
- Regex regex = new Regex(@"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$", RegexOptions.IgnoreCase);
- Match match = regex.Match(email);
- if (match.Success)
- return true;
- else
- return false;
- }
- // Method for creating the password key
- private string key()
- {
- var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
- var stringChars = new char[10];
- var random = new Random();
- for (int i = 0; i < stringChars.Length; i++)
- {
- stringChars[i] = chars[random.Next(chars.Length)];
- }
- var key = new String(stringChars);
- return key;
- }
- // Method for hashing the password
- private string GetHashedString(string pass, string salt)
- {
- string hashedPass;
- SHA512 sha = new SHA512CryptoServiceProvider();
- byte[] result;
- StringBuilder strBuilder = new StringBuilder();
- // Hashes the password along with the random salt
- sha.ComputeHash(ASCIIEncoding.ASCII.GetBytes(pass + salt));
- result = sha.Hash;
- for (int i = 0; i < result.Length; i++)
- {
- strBuilder.Append(result[i].ToString("x2"));
- }
- hashedPass = strBuilder.ToString();
- return hashedPass;
- }
- public void btnRegister_Click(object sender, EventArgs e)
- {
- using (conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConn"].ToString()))
- {
- string Name = txtName.Text;
- string Email = txtEmail.Text;
- string Pass = txtPass.Text;
- string RePass = txtPassRepeat.Text;
- if(Name != string.Empty && Email != string.Empty && Pass != string.Empty && RePass != string.Empty)
- {
- if (ValidateEmail(Email))
- {
- if (Pass == RePass)
- {
- // Tjek om mailen allerede er brugt
- using (SqlCommand CheckEmail = new SqlCommand("SELECT * FROM users WHERE userEmail = @mail", conn))
- {
- CheckEmail.Parameters.AddWithValue("@mail", Email);
- conn.Open();
- SqlDataReader reader = CheckEmail.ExecuteReader();
- if (!reader.HasRows)
- {
- reader.Close();
- using (SqlCommand registerUser = new SqlCommand("INSERT INTO users (userName, userEmail, userPass, userKey) VALUES ()", conn))
- {
- string keyCollection = key();
- string hashedPass = GetHashedString(Pass, keyCollection);
- registerUser.Parameters.AddWithValue("@name", Name);
- registerUser.Parameters.AddWithValue("@mail", Email);
- registerUser.Parameters.AddWithValue("@pass", hashedPass);
- registerUser.Parameters.AddWithValue("@key", keyCollection);
- registerUser.ExecuteNonQuery();
- }
- }
- else
- {
- litAlert.Text = "This e-mail is allready in use";
- panAlert.Visible = true;
- }
- conn.Close();
- }
- }
- else
- {
- litAlert.Text = "The passwords doesn't match";
- panAlert.Visible = true;
- }
- }
- else
- {
- litAlert.Text = "Please type your real e-mail adress";
- panAlert.Visible = true;
- }
- }
- else
- {
- litAlert.Text = "Please fill all the fields.";
- panAlert.Visible = true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement