Advertisement
Guest User

Untitled

a guest
May 25th, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.38 KB | None | 0 0
  1. // Method for validating the e-mail
  2.     public bool ValidateEmail(string emailaddress)
  3.     {
  4.         string email = emailaddress;
  5.         Regex regex = new Regex(@"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$", RegexOptions.IgnoreCase);
  6.         Match match = regex.Match(email);
  7.         if (match.Success)
  8.             return true;
  9.         else
  10.             return false;
  11.     }
  12.  
  13.     // Method for creating the password key
  14.     private string key()
  15.     {
  16.         var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  17.         var stringChars = new char[10];
  18.         var random = new Random();
  19.  
  20.         for (int i = 0; i < stringChars.Length; i++)
  21.         {
  22.             stringChars[i] = chars[random.Next(chars.Length)];
  23.         }
  24.  
  25.         var key = new String(stringChars);
  26.  
  27.         return key;
  28.     }
  29.  
  30.     // Method for hashing the password
  31.     private string GetHashedString(string pass, string salt)
  32.     {
  33.         string hashedPass;
  34.         SHA512 sha = new SHA512CryptoServiceProvider();
  35.         byte[] result;
  36.         StringBuilder strBuilder = new StringBuilder();
  37.  
  38.         // Hashes the password along with the random salt
  39.         sha.ComputeHash(ASCIIEncoding.ASCII.GetBytes(pass + salt));
  40.         result = sha.Hash;
  41.  
  42.         for (int i = 0; i < result.Length; i++)
  43.         {
  44.             strBuilder.Append(result[i].ToString("x2"));
  45.         }
  46.  
  47.         hashedPass = strBuilder.ToString();
  48.         return hashedPass;
  49.     }
  50.  
  51. public void btnRegister_Click(object sender, EventArgs e)
  52.     {
  53.         using (conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConn"].ToString()))
  54.         {
  55.             string Name = txtName.Text;
  56.             string Email = txtEmail.Text;
  57.             string Pass = txtPass.Text;
  58.             string RePass = txtPassRepeat.Text;
  59.  
  60.             if(Name != string.Empty && Email != string.Empty && Pass != string.Empty && RePass != string.Empty)
  61.             {
  62.                 if (ValidateEmail(Email))
  63.                 {
  64.                     if (Pass == RePass)
  65.                     {
  66.                         // Tjek om mailen allerede er brugt
  67.                         using (SqlCommand CheckEmail = new SqlCommand("SELECT * FROM users WHERE userEmail = @mail", conn))
  68.                         {
  69.                             CheckEmail.Parameters.AddWithValue("@mail", Email);
  70.  
  71.                             conn.Open();
  72.                             SqlDataReader reader = CheckEmail.ExecuteReader();
  73.                             if (!reader.HasRows)
  74.                             {
  75.                                 reader.Close();
  76.  
  77.                                 using (SqlCommand registerUser = new SqlCommand("INSERT INTO users (userName, userEmail, userPass, userKey) VALUES ()", conn))
  78.                                 {
  79.                                     string keyCollection = key();
  80.                                     string hashedPass = GetHashedString(Pass, keyCollection);
  81.  
  82.                                     registerUser.Parameters.AddWithValue("@name", Name);
  83.                                     registerUser.Parameters.AddWithValue("@mail", Email);
  84.                                     registerUser.Parameters.AddWithValue("@pass", hashedPass);
  85.                                     registerUser.Parameters.AddWithValue("@key", keyCollection);
  86.                                    
  87.                                     registerUser.ExecuteNonQuery();
  88.                                 }
  89.                             }
  90.                             else
  91.                             {
  92.                                 litAlert.Text = "This e-mail is allready in use";
  93.                                 panAlert.Visible = true;
  94.                             }
  95.                             conn.Close();
  96.                         }
  97.                     }
  98.                     else
  99.                     {
  100.                         litAlert.Text = "The passwords doesn't match";
  101.                         panAlert.Visible = true;
  102.                     }
  103.                 }
  104.                 else
  105.                 {
  106.                     litAlert.Text = "Please type your real e-mail adress";
  107.                     panAlert.Visible = true;
  108.                 }
  109.             }
  110.             else
  111.             {
  112.                 litAlert.Text = "Please fill all the fields.";
  113.                 panAlert.Visible = true;
  114.             }
  115.         }
  116.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement