public ActionResult SignUp([FromBody] SignUpPageViewModel model) { var redirectUrl = ""; if (!ModelState.IsValid) { redirectUrl = Url.Action("Index", "SignUp"); //return RedirectToAction("Index", "SignUp"); return(Json(new { redirectUrl })); } byte[] salt; new RNGCryptoServiceProvider().GetBytes(salt = new byte[16]); var passwordEncryption = new PasswordEncription(); string encryptPassword = passwordEncryption.EncryptPassword(model.Password, configuration["AppSettings:PasswordKey"]); Users user = new Users(); user.Email = model.Email; user.Password = encryptPassword; user.Username = model.Username; user.Role = Roles.User; user.Salt = Encoding.UTF8.GetString(salt, 0, salt.Length); var userDb = db.Users.Where(x => x.Email == model.Email).FirstOrDefault(); if (userDb != null) { //throw an error } using (var db = new ApplicationDbContext()) { var users = db.Set <Users>(); users.Add(user); db.SaveChanges(); } var identity = new ClaimsIdentity(new[] { new Claim(ClaimTypes.Email, user.Email) }, CookieAuthenticationDefaults.AuthenticationScheme); var principal = new ClaimsPrincipal(identity); var claim = new Claim(ClaimTypes.Role, "User"); identity.AddClaim(claim); var login = HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal); redirectUrl = Url.Action("Index", "Home"); //return RedirectToAction("Index", "Home"); return(Json(new { redirectUrl })); }
public async Task <ActionResult> Login([FromBody] LoginPageViewModel model) { if (!ModelState.IsValid) { var redirectUrl = Url.Action("Index", "Login"); //return RedirectToAction("Index", "Login"); return(Json(new { redirectUrl })); } Users user = new Users() { Email = model.Email, Password = model.Password }; var passwordEncryption = new PasswordEncription(); user = db.Users.Where(x => x.Email == model.Email).FirstOrDefault(); var decryptedPass = passwordEncryption.DecryptPassword(user.Password, configuration["AppSettings:PasswordKey"]); if (model.Password != decryptedPass) { user = null; } if (user != null) { var identity = new ClaimsIdentity(new[] { new Claim(ClaimTypes.Email, user.Email) }, CookieAuthenticationDefaults.AuthenticationScheme); if (user.Email == "*****@*****.**") { var claim = new Claim(ClaimTypes.Role, "Admin"); identity.AddClaim(claim); } var principal = new ClaimsPrincipal(identity); await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal); var redirectUrl = Url.Action("Index", "Synonyms"); //return RedirectToAction("Index", "Synonyms"); return(Json(new { redirectUrl })); } else { ModelState.AddModelError("", "Invalid login attempt."); var redirectUrl = Url.Action("Index", "Login"); //return RedirectToAction("Index", "Login"); return(Json(new { redirectUrl })); } }
public ActionResult CreateUser([FromBody] SignUpPageViewModel model) { var redirectUrl = ""; if (!ModelState.IsValid) { redirectUrl = Url.Action("Index", "SignUp"); return(Json(new { MessageТype = MessageType.Error, Message = "Error" })); } byte[] salt; new RNGCryptoServiceProvider().GetBytes(salt = new byte[16]); var passwordEncryption = new PasswordEncription(); string encryptPassword = passwordEncryption.EncryptPassword(model.Password, "E546C8DF278CD5931069B522E695D4F2"); Users user = new Users(); user.Email = model.Email; user.Password = encryptPassword; user.Username = model.Username; user.Role = Roles.User; user.Salt = Encoding.UTF8.GetString(salt, 0, salt.Length); var userDb = db.Users.Where(x => x.Email == model.Email).FirstOrDefault(); if (userDb != null) { //throw an error } using (var db = new ApplicationDbContext()) { var users = db.Set <Users>(); users.Add(user); db.SaveChanges(); } return(Json(new { MessageТype = MessageType.Success, Message = "Success" })); }
public bool ValidateLogin(string username, string password) { return(_userRepository.GetAllUsers().Any(u => username == u.Username && PasswordEncription.VerifyHashedPassword(u.Password, password))); }
public void Add(string username, string password) { _userRepository.Add(username, PasswordEncription.HashPassword(password)); }