public async Task <GenericResponse <string> > AddManager(Manager manager) { try { if (string.IsNullOrWhiteSpace(manager.PassportNumber) && string.IsNullOrWhiteSpace(manager.IcNumber)) { return(new GenericResponse <string>(false, null, "Passport or IC number is required")); } if (!await IsEmailAvailable(manager.Email)) { return(new GenericResponse <string>(false, null, "Email is not available.")); } if (!await IsUserNameAvailable(manager.UserName)) { return(new GenericResponse <string>(false, null, "Username is not available.")); } manager.UserId = Guid.NewGuid().ToString(); manager.Password = _passwordHasher.GetHashedPassword(manager.Password); manager.JoinDate = (long)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1, 0, 0, 0)).TotalMilliseconds; manager.Status = (int)ManagerStatus.Active; var response = await _managerRepository.AddManager(manager); if (!response) { return(new GenericResponse <string>(false, null, "Error adding user")); } return(new GenericResponse <string>(true, null, "Manager added successfully.")); } catch (Exception exception) { throw new Exception("Error occured" + exception); } }
private void Default_OK_Click(object sender, EventArgs e) { if (F_OAuth_TB_Password.Text != F_OAuth_TB_Password2.Text) { return; } if (F_OAuth_L_Password.Text == String.Empty || F_OAuth_TB_Password2.Text == String.Empty) { return; } PasswordManager pwManager = new PasswordManager(); PasswordHasher hasher = new PasswordHasher(); string password = hasher.GetHashedPassword(F_OAuth_TB_Password.Text); if (!hasher.CheckPassword(F_OAuth_TB_Password.Text, password)) { return; } _settings.AddOrChangeKeyValue("OAuthPassword", password); _settings.AddOrChangeKeyValue("OauthKey", pwManager.EncryptPassword(F_OAuth_TB_Key.Text, F_OAuth_TB_Password.Text)); _settings.AddOrChangeKeyValue("OAuthSecret", pwManager.EncryptPassword(F_OAuth_TB_Secret.Text, F_OAuth_TB_Password.Text)); _settings.Save(); _g2g = true; this.Close(); }
private static void SeedData(FitterDbContext dbContext) { var user = new User { FirstName = "Adrian", LastName = "Boros", Email = "*****@*****.**", Password = "******", }; var passwordHasher = new PasswordHasher(user.Password); user.Password = passwordHasher.GetHashedPassword(); dbContext.Users.Add(user); dbContext.SaveChanges(); var user2 = new User { FirstName = "Matej", LastName = "Zahorsky", Email = "*****@*****.**", Password = "******", }; var passwordHasher2 = new PasswordHasher(user2.Password); user2.Password = passwordHasher2.GetHashedPassword(); dbContext.Users.Add(user2); dbContext.SaveChanges(); }
public void Add(User user) { var passwordHasher = new PasswordHasher(user.Password); user.Password = passwordHasher.GetHashedPassword(); _context.Add(user); }
public UserDetailModel Create(UserDetailModel user) { using (var dbContext = _fitterDbContext.CreateDbContext()) { var passwordHasher = new PasswordHasher(user.Password); user.Password = passwordHasher.GetHashedPassword(); var entity = _mapper.MapUserToEntity(user); dbContext.Users.Add(entity); dbContext.SaveChanges(); return(_mapper.MapUserDetailModelFromEntity(entity)); } }
/* * @Method: registerBtn_Click * @Params: object sender, EventArgs e * @Return: void * @Description: This method will be activated when Add Customer button is * clicked. The method will create a new customer object, hash the password, * and add the customer to the database accordingly */ protected void registerBtn_Click(object sender, EventArgs e) { Customer customer = new Customer(); //Creating new ID for the customer customer.CustomerID = Guid.NewGuid(); //Getting new hashed password PasswordHasher hasher = new PasswordHasher(); string hashedPassword = hasher.GetHashedPassword(RegisterUserPasswordInput.Text, RegisterUserMailInput.Text); //Constructing the insert query string query = "insert into Customers values('" + customer.CustomerID + "','" + RegisterUserNameInput.Text + "','" + RegisterUserMailInput.Text + "','" + RegisterUserPhoneInput.Text + "','" + hashedPassword + "')"; string result = dbCommander.InsertRecord(query); if (result == "1") { //Closing database connection dbCommander.CloseConnection(); //Building success message script to redirect after success StringBuilder builder = new StringBuilder(); builder.Append("alert('Account created successfully');"); builder.Append("window.location.href = '"); builder.Append(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority)); builder.Append("/Admin/AdminManageCustomer.aspx"); builder.Append("';"); builder.ToString(); } else { //If the user is not inserted, show that there was an error with the database ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Alert", "alert('Database error, please check your provided data to be correct');", true); //Closing database connection dbCommander.CloseConnection(); } }
/* * @Method: AddAdminBtn_Click * @Params: object sender, EventArgs e * @Return: void * @Description: This method will be activated on the click of submit admin button. * A new ID will be assigned to the new admin, and password will be hashed, and * saved in the database using SQL Data Access Layer */ protected void AddAdminBtn_Click(object sender, EventArgs e) { AdminObject admin = new AdminObject(); //Creating new unique ID to the admin object admin.AdminID = Guid.NewGuid(); //Getting new hashed password PasswordHasher hasher = new PasswordHasher(); string hashedPassword = hasher.GetHashedPassword(AdminPasswordInput.Text, AdminEmailInput.Text); //Constructing the insert query string query = "insert into tblAdmins values('" + admin.AdminID + "','" + AdminNameInput.Text + "','" + AdminEmailInput.Text + "','" + hashedPassword + "')"; string result = dbCommander.InsertRecord(query); if (result == "1") { //Success message Response.Write("<script>alert('Admin inserted');</script>"); //Clearing all the input fields AdminNameInput.Text = string.Empty; AdminEmailInput.Text = string.Empty; AdminPasswordInput.Text = string.Empty; } else { //If the user is not inserted, show that there was an error with the database Response.Write ("<script>alert('Database error, please check your provided data to be correct');</script>"); } //Closing connection to database dbCommander.CloseConnection(); //Refresh the GridView AdminGridView.DataBind(); }
/* * @Method: registerButton_CLick * @Params: object sender, EventArgs e * @Return: void * @Description: This method will be activated when the user clicks Register button, * it will collect his data, hash his password, and register the user in the database. */ protected void registerButton_Click(object sender, EventArgs e) { Guid id = Guid.NewGuid(); PasswordHasher hasher = new PasswordHasher(); string hashedPassword = hasher.GetHashedPassword(passwordInput.Text, idInput.Text); //Constructing the insert query string query = "insert into tblUsers values('" + id + "','" + nameInput.Text + "','" + idInput.Text + "','" + hashedPassword + "')"; string result = dbCommander.InsertRecord(query); if (result == "1") { //Closing database connection dbCommander.CloseConnection(); registerStatus.Text = "User Created Successfully"; } }
protected void loginBtn_Click(object sender, EventArgs e) { try { //Hashing the given password by admin for comparison PasswordHasher hasher = new PasswordHasher(); string hashedPassword = hasher.GetHashedPassword(passwordInput.Text, emailInput.Text); //Building read query string query = "select * from tblAdmins where Email='" + emailInput.Text + "' and PasswordHash='" + hashedPassword + "'"; //Getting reader result var reader = dbCommander.ReadRecord(query); //Check if the reader has returned rows or not if (reader.HasRows) { AdminObject requestedAdmin = new AdminObject(); while (reader.Read()) { requestedAdmin.Name = (String)reader["Name"]; requestedAdmin.Email = (String)reader["Email"]; requestedAdmin.AdminID = (Guid)reader["AdminID"]; } //Admin info is added to the session Session["Admin"] = requestedAdmin; //Create Authentication ticket FormsAuthenticationTicket ticket; string cookieInfo; HttpCookie adminCookie; //Stay logged in ticket if (stayLogged.Checked) { ticket = new FormsAuthenticationTicket(requestedAdmin.Email, true, 60); } //Not to stay logged in ticket else { ticket = new FormsAuthenticationTicket(requestedAdmin.Email, false, 1); } //Adding the authentication ticket to a cookie cookieInfo = FormsAuthentication.Encrypt(ticket); adminCookie = new HttpCookie(FormsAuthentication.FormsCookieName, cookieInfo); adminCookie.Expires = ticket.Expiration; adminCookie.Path = FormsAuthentication.FormsCookiePath; Response.Cookies.Add(adminCookie); //Closing database connection dbCommander.CloseConnection(); //Redirecting to AdminManagement page Response.Redirect("/Admin/AdminManagement.aspx"); } else { throw new NullReferenceException(); } } catch (NullReferenceException) { //If the the credentials are not correct, show error Response.Write ("<script>alert('Wrong credentials, please check your provided data to be correct');</script>"); //Closing connection after exception dbCommander.CloseConnection(); } }
/* * @Method: loginBtn_Click * @Params: object sender, EventArgs e * @Return: void * @Description: This method will be activated on the click of login customer button. * The customer data will be compared to the database and create a cookie for the user */ protected void loginBtn_Click(object sender, EventArgs e) { try { //Hashing the given password by admin for comparison PasswordHasher hasher = new PasswordHasher(); string hashedPassword = hasher.GetHashedPassword(LoginUserPasswordInput.Text, LoginUserEmailInput.Text); //Building read query string query = "select * from Customers where CustomerMail='" + LoginUserEmailInput.Text + "' and HashedPassword='******'"; //Getting reader result var reader = dbCommander.ReadRecord(query); //Check if the reader has returned rows or not if (reader.HasRows) { Customer customer = new Customer(); while (reader.Read()) { customer.CustomerID = (Guid)reader["CustomerID"]; customer.CustomerName = (string)reader["CustomerName"]; customer.CustomerEmail = (string)reader["CustomerMail"]; } //Customer info is added to session Session["Customer"] = customer; //Create Authentication ticket FormsAuthenticationTicket ticket; string cookieInfo; HttpCookie customerCookie; ticket = new FormsAuthenticationTicket(customer.CustomerEmail, true, 60); //Adding the authentication ticket to a cookie cookieInfo = FormsAuthentication.Encrypt(ticket); customerCookie = new HttpCookie(FormsAuthentication.FormsCookieName, cookieInfo); customerCookie.Expires = ticket.Expiration; customerCookie.Path = FormsAuthentication.FormsCookiePath; Response.Cookies.Add(customerCookie); //Closing database connection dbCommander.CloseConnection(); //Building success message script to redirect after success StringBuilder builder = new StringBuilder(); builder.Append("<script>alert('Successfully logged in');"); builder.Append("window.location.href = '"); builder.Append(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority)); builder.Append("/default.aspx"); builder.Append("';</script>"); Session["Basket"] = null; Response.Write(builder.ToString()); } else { throw new NullReferenceException(); } } catch (NullReferenceException) { //If the the credentials are not correct, show error Response.Write ("<script>alert('Wrong credentials, please check your provided data to be correct');</script>"); //Closing connection after exception dbCommander.CloseConnection(); } }
protected override void OnModelCreating(ModelBuilder bldr) { bldr.Entity <Statistics>() .HasOne(t => t.Tournament) .WithMany(t => t.Statistics) .OnDelete(DeleteBehavior.Cascade); bldr.Entity <Match>() .HasOne(t => t.Tournament) .WithMany(t => t.Matches) .OnDelete(DeleteBehavior.Cascade); bldr.Entity <UsersInMatch>() .HasOne(t => t.Match) .WithMany(t => t.UsersInMatches) .OnDelete(DeleteBehavior.Cascade); bldr.Entity <TeamsInMatch>() .HasOne(t => t.Match) .WithMany(t => t.TeamsInMatches) .OnDelete(DeleteBehavior.Cascade); var password1 = new PasswordHasher("purkyne"); var password2 = new PasswordHasher("netflix"); var password3 = new PasswordHasher("heslicko"); var password4 = new PasswordHasher("conan"); bldr.Entity <User>() .HasData(new { UserId = 1, FirstName = "Daniel", LastName = "Weis", Email = "*****@*****.**", Password = password1.GetHashedPassword(), TeamId = 1, isAdmin = false }, new { UserId = 2, FirstName = "Walter", LastName = "White", Email = "*****@*****.**", Password = password2.GetHashedPassword(), TeamId = 1, isAdmin = false }, new { UserId = 3, FirstName = "Adam", LastName = "Pered", Email = "*****@*****.**", Password = password3.GetHashedPassword(), TeamId = 2, isAdmin = true }, new { UserId = 4, FirstName = "Jordan", LastName = "Schlansky", Email = "*****@*****.**", Password = password4.GetHashedPassword(), TeamId = 2, isAdmin = false });; bldr.Entity <Team>() .HasData(new { TeamId = 1, Name = "Sicaci", Logo = 1 }, new { TeamId = 2, Name = "CastroTeam", Logo = 2 }); bldr.Entity <Tournament>() .HasData(new { TournamentId = 1, Name = "FIT - BIT", Location = "Bozetechova", Prize = 500, Entry = 5, Capacity = 16, Type = "Duo", Organizer = "Daniel Weis", Date = "2019-10-31", Time = "14:00", Sponsors = "Coca Cola", Referee = "Adam Pered" }, new { TournamentId = 2, Name = "FIT - MIT", Location = "Bozetechova", Prize = 1000, Entry = 100, Capacity = 8, Type = "Solo", Organizer = "Alfonz Hrozny", Date = "2019-10-30", Time = "14:00", Sponsors = "Pepsi, Hyundai", Referee = "Daniel Weis" }); bldr.Entity <Participant>() .HasData(new { ParticipantId = 1, Name = "Daniel Weis", UserOrTeam = 1, IsUser = true, TournamentId = 2 }, new { ParticipantId = 2, Name = "Walter White", UserOrTeam = 2, IsUser = true, TournamentId = 2 }, new { ParticipantId = 3, Name = "Sicaci", UserOrTeam = 1, IsUser = false, TournamentId = 1 }, new { ParticipantId = 4, Name = "CastroTeam", UserOrTeam = 2, IsUser = false, TournamentId = 1 }); bldr.Entity <Statistics>() .HasData(new { StatisticsId = 1, Goals = 5, Games = 2, Wins = 1, Draws = 1, Loses = 0, UserId = 1 }, new { StatisticsId = 2, Goals = 0, Games = 5, Wins = 0, Draws = 3, Loses = 2, UserId = 2 }, new { StatisticsId = 3, Goals = 4, Games = 5, Wins = 0, Draws = 3, Loses = 2, UserId = 3 }, new { StatisticsId = 4, Goals = 0, Games = 0, Wins = 0, Draws = 0, Loses = 0, UserId = 4 }, new { StatisticsId = 5, Goals = 9, Games = 1, Wins = 1, Draws = 0, Loses = 0, Team = "Sicaci" }, new { StatisticsId = 6, Goals = 1, Games = 2, Wins = 1, Draws = 0, Loses = 1, Team = "CastroTeam" }, new { StatisticsId = 7, Goals = 1, Games = 1, Wins = 0, Draws = 1, Loses = 0, UserId = 1, TournamentId = 2 }, new { StatisticsId = 8, Goals = 1, Games = 1, Wins = 0, Draws = 1, Loses = 0, UserId = 2, TournamentId = 2 }, new { StatisticsId = 9, Goals = 9, Games = 1, Wins = 1, Draws = 0, Loses = 0, Team = "Sicaci", TournamentId = 1 }, new { StatisticsId = 10, Goals = 0, Games = 1, Wins = 0, Draws = 0, Loses = 1, Team = "CastroTeam", TournamentId = 1 }); bldr.Entity <Match>() .HasData(new { MatchId = 1, HomeScore = 1, AwayScore = 2, HomeUserId = 1, AwayUserId = 2, Winner = "Away", TournamentId = 2, Round = 1 }, new { MatchId = 2, HomeScore = 9, AwayScore = 0, Winner = "Home", HomeTeam = "Sicaci", AwayTeam = "CastroTeam", TournamentId = 1, Round = 1 }); bldr.Entity <UsersInMatch>() .HasData(new { UsersInMatchId = 1, Home = true, UserId = 1, MatchId = 1 }, new { UsersInMatchId = 2, Home = false, UserId = 2, MatchId = 1 }); bldr.Entity <TeamsInMatch>() .HasData(new { TeamsInMatchId = 1, Home = true, TeamId = 1, MatchId = 2 }, new { TeamsInMatchId = 2, Home = false, TeamId = 2, MatchId = 2 }); }
public void GetHashedPasswordNoSaltTest() { string myPassword = "******"; string hashedPassword = hasher.GetHashedPassword(myPassword); Assert.IsTrue(hasher.CheckPassword(myPassword, hashedPassword)); }