private void BtnRegister_Click(object sender, EventArgs e) { //Regex for checking username var userNameRegex = new Regex("^[a-zA-Z][a-zA-Z0-5]{5,}$"); //Regex for checking name var nameRegex = new Regex("^[a-zA-Z][a-zA-Z ]{4,}$"); //Regex for checking password var passwordRegex = new Regex("^(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{8,20})$"); //Checking whether the given input data is valid or not by using corresponding regex if (!nameRegex.IsMatch(TextboxName.Text) || !userNameRegex.IsMatch(TextboxUsername.Text) || !passwordRegex.IsMatch(TextboxPassword.Text) || !passwordRegex.IsMatch(TextboxPassConfirm.Text) || TextboxPassword.Text != TextboxPassConfirm.Text) { //Checking whether given name is valid if (!nameRegex.IsMatch(TextboxName.Text)) { MessageBox.Show("Please enter a valid Name! **At Least 5 characters and space between name are only allowed**"); } //Checking whether given username is valid else if (!userNameRegex.IsMatch(TextboxUsername.Text)) { MessageBox.Show("Please enter a valid User Name! **At Least 6 characters(alphabets and digits only) are required and must present alphabet and digits**"); } //Checking whether given name is valid else if (!passwordRegex.IsMatch(TextboxPassword.Text)) { MessageBox.Show("Please enter a valid Password!**At Least 8 and At most 20 characters(alphabets and digits only) are required and must have at least an alphabet and a digit.**"); } //Checking whether given password is valid else if (!passwordRegex.IsMatch(TextboxPassConfirm.Text)) { MessageBox.Show("Please enter a valid Confirm Password!**At Least 8 and At most 20 characters(alphabets and digits only) are required and must have at least an alphabet and a digit**"); } //Checking whether given confirm password is valid else if (TextboxPassword.Text != TextboxPassConfirm.Text) { MessageBox.Show("Password and Confirm Password Doesn't Match!"); } else { //for future logic } } //Given data is valid to be inserted into database else { //Creating string instance of the given username string uname = TextboxUsername.Text; //creating local instance of database context to communicate withe database using (RechargeGauntletDataContext _context = new RechargeGauntletDataContext()) { //Checking if the given username already exists in the database if (_context.Users.Count(u => u.Username == uname) != 0) { MessageBox.Show("Username already Exists!"); } //Username doesn't exists in the database else { //Creating string instance of the given password string password = TextboxPassword.Text; //Creating string instance of the hashed version of given password string hashedPassword = PasswordHasher.Hash(password); //creating a new User Instance to be inserted to the User Table User user = new User { //Initializing properties of User Username = uname, Name = TextboxName.Text, PasswordHash = hashedPassword }; //Adding new Instance to the instance of database. _context.Users.InsertOnSubmit(user); //Saving changes _context.SubmitChanges(); MessageBox.Show("Registered!"); } } } }
private void BtnLogin_Click(object sender, EventArgs e) { //Regex to check Username var userNameRegex = new Regex("^[a-zA-Z][a-zA-Z0-5]{5,}$"); //Regex to check Password var passwordRegex = new Regex("^(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{8,20})$"); //Checking first if the username and password is valid if (!userNameRegex.IsMatch(TextboxUserName.Text) || !passwordRegex.IsMatch(TextboxPassword.Text)) { //Username is found not valid if (!userNameRegex.IsMatch(TextboxUserName.Text)) { MessageBox.Show("Please enter a valid User Name! **At Least 6 characters(alphabets and digits only) are required and must present alphabet and digits**"); } //Password is found not valid else if (!passwordRegex.IsMatch(TextboxPassword.Text)) { MessageBox.Show("Please enter a valid Password!**At Least 8 and At most 20 characters(alphabets and digits only) are required and must have at least an alphabet and a digit.**"); } else { //For future validations } } //Username and password are found valid, now checking if they are correct by matching them with database existing pairs. else { //Creating a local instance of Database context class using (RechargeGauntletDataContext _context = new RechargeGauntletDataContext()) { //Fetching the particular record of the given username var user = (from u in _context.Users where u.Username == TextboxUserName.Text select u).FirstOrDefault(); //Checking if the given username exists in the database if (user == null) { MessageBox.Show("Username doesn't exist!"); } //Given Username exists in the database else { //creating a hashed password's string instance. string hashedPassword = user.PasswordHash; //Checking whether the given password matches with the existing hashed password if (PasswordHasher.Verify(TextboxPassword.Text, hashedPassword)) { MessageBox.Show("Logging In!"); } //Passwords doesn't match else { MessageBox.Show("Incorrect Password!"); } } } } }