public async Task <IHttpActionResult> Login(LoginModel model) { if (ModelState.IsValid) { var user = await AppUserManager.FindByEmailAsync(model.Email); //We don't want a deleted user to be able to log in. if (user != null && !user.IsDeleted) { var valid = await AppUserManager.CheckPasswordAsync(user, model.Password); if (valid) { await SignInAsync(user, model.RememberMe); return(Ok()); } } ModelState.AddModelError("", "Invalid username or password."); } // If we got this far, something failed, redisplay form return(BadRequest(ModelState)); }
public async Task <IActionResult> OnPostAsync() { var user = await _userManager.GetUserAsync(User); if (user == null) { return(NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.")); } RequirePassword = await _userManager.HasPasswordAsync(user); if (RequirePassword) { if (!await _userManager.CheckPasswordAsync(user, Input.Password)) { ModelState.AddModelError(string.Empty, "Password not correct."); return(Page()); } } var result = await _userManager.DeleteAsync(user); var userId = await _userManager.GetUserIdAsync(user); if (!result.Succeeded) { throw new InvalidOperationException($"Unexpected error occurred deleteing user with ID '{userId}'."); } await _signInManager.SignOutAsync(); _logger.LogInformation("User with ID '{UserId}' deleted themselves.", userId); return(Redirect("~/")); }
public async Task <HttpResponseMessage> ChangePassword(HttpRequestMessage request, string id, string password, string newPassword) { if (User != null && MemoryCacheHelper.RemoveUserEditByAdmin(User.Identity.Name)) { return(request.CreateErrorResponse(HttpStatusCode.BadRequest, CommonConstants.Error_Edit_By_Admin)); } if (!string.IsNullOrEmpty(id)) { var appUser = await AppUserManager.FindByIdAsync(id); if (appUser != null) { bool passwordCheck = await AppUserManager.CheckPasswordAsync(appUser, password); if (passwordCheck) { var result = await AppUserManager.ChangePasswordAsync(appUser.Id, password, newPassword); return(request.CreateResponse(HttpStatusCode.OK, Common.Constants.MessageSystem.ChangePasswordSuccess)); } return(request.CreateErrorResponse(HttpStatusCode.BadRequest, Common.Constants.MessageSystem.ErrorOldPassword)); } return(request.CreateErrorResponse(HttpStatusCode.BadRequest, Common.Constants.MessageSystem.UserNotFound)); } return(request.CreateErrorResponse(HttpStatusCode.BadRequest, Common.Constants.MessageSystem.ErrorIdNull)); }
public async Task <ActionResult> ChangePassword(UserChangePasswordViewModel model) { if (!ModelState.IsValid) { Response.StatusCode = (int)HttpStatusCode.BadRequest; return(PartialView(model)); } if (model.NewPassword != model.NewPasswordCopy) { ModelState.AddModelError("", "The new password and confirmation password does not match."); Response.StatusCode = (int)HttpStatusCode.BadRequest; return(PartialView(model)); } AppUser user = GetCurrentUser(); bool correctPass = await _userManager.CheckPasswordAsync(user, model.OldPassword); if (!correctPass) { ModelState.AddModelError("", "Please enter valid current password."); Response.StatusCode = (int)HttpStatusCode.BadRequest; return(PartialView(model)); } IdentityResult validPass = await _userManager.PasswordValidator.ValidateAsync(model.NewPassword); if (validPass.Succeeded) { user.PasswordHash = _userManager.PasswordHasher.HashPassword(model.NewPassword); } else { AddErrorsFromResult(validPass); } if (validPass == null || (model.NewPassword != string.Empty && validPass.Succeeded)) { IdentityResult result = await _userManager.UpdateAsync(user); if (result.Succeeded) { _mailingRepository.PasswordChangedMail(user.Email); NotifyManager.Set("notification-success", "Success!", "Your password has been changed!"); return(Json(new { url = Url.Action("Index") })); } else { AddErrorsFromResult(result); } } else { ModelState.AddModelError("", "User Not Found"); } Response.StatusCode = (int)HttpStatusCode.BadRequest; return(PartialView(model)); }
public async Task <IActionResult> LogIn([FromForm] UserLogInModel userLogIn) { AppUser user = await AppUserManager.FindByNameAsync(userLogIn.UserName); if (user != null) { if (await AppUserManager.CheckPasswordAsync(user, userLogIn.Password)) { await AppSignInManager.SignInAsync(user, isPersistent : true); return(Json(new { success = true })); } } return(Json(new { success = false })); }
public async Task SignIn(UserLoginModel model) { if (model == null) { return; } var user = await _appUserManager.FindByEmailAsync(model.Email); if (user == null) { return; } var isPassword = await _appUserManager.CheckPasswordAsync(user, model.Password); var s = isPassword; }
public async Task <IHttpActionResult> Login(LoginBindingModel LoginModel) { var ctx = ApplicationDbContext.Create(); if (string.IsNullOrWhiteSpace(LoginModel.username) || string.IsNullOrWhiteSpace(LoginModel.password)) { return(NotFound()); } var user = await AppUserManager.FindByNameAsync(LoginModel.username); if (user == null) { return(NotFound()); } var passwordSignInResult = await AppUserManager.CheckPasswordAsync(user, LoginModel.password); if (!passwordSignInResult) { ctx.Login_Logs.Add(new Login_Logs() { Ip_Address = Request.GetOwinContext().Request.RemoteIpAddress, Status = "The password is incorrect.", Time = DateTime.Now, UserId = user.Id, }); await ctx.SaveChangesAsync(); return(NotFound()); } //Get Token from ~//oauth/token HttpClient client = new HttpClient(); client.BaseAddress = new Uri(Startup.address); client.DefaultRequestHeaders .Accept .Add(new MediaTypeWithQualityHeaderValue("application/json")); client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json; charset=utf-8"); var json = new JavaScriptSerializer().Serialize(LoginModel); HttpResponseMessage response = await client.PostAsJsonAsync("/oauth/token", json); if (response.StatusCode == System.Net.HttpStatusCode.OK) { ctx.Login_Logs.Add(new Login_Logs() { Ip_Address = Request.GetOwinContext().Request.RemoteIpAddress, Status = "Login Successful.", Time = DateTime.Now, UserId = user.Id, }); await ctx.SaveChangesAsync(); return(Ok(response.Content)); } else { return(BadRequest(response.ReasonPhrase)); } }