public async Task <IActionResult> OnPostAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); if (ModelState.IsValid) { // This doesn't count login failures towards account lockout // To enable password failures to trigger account lockout, set lockoutOnFailure: true var result = await signInService.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure : true); if (result.Succeeded) { logger.LogInformation("User logged in."); return(LocalRedirect(returnUrl)); } if (result.RequiresTwoFactor) { return(RedirectToPage("./LoginWith2fa", new { ReturnUrl = returnUrl, RememberMe = Input.RememberMe })); } if (result.IsLockedOut) { logger.LogWarning("User account locked out."); return(RedirectToPage("./Lockout")); } else { ModelState.AddModelError(string.Empty, "Invalid login attempt."); return(Page()); } } // If we got this far, something failed, redisplay form return(Page()); }
public async Task <IActionResult> OnPostAsync(string returnUrl = null) { returnUrl ??= Url.Content("~/"); // This doesn't count login failures towards account lockout // To enable password failures to trigger account lockout, set lockoutOnFailure: true var result = await _signInService.PasswordSignInAsync(Input.Username, Input.Password, Input.RememberMe, lockoutOnFailure : true); if (result.Succeeded) { return(LocalRedirect(returnUrl)); } if (result.RequiresTwoFactor) { return(RedirectToPage("./LoginWith2fa", new { ReturnUrl = returnUrl, Input.RememberMe })); } if (result.IsLockedOut) { return(RedirectToPage("./Lockout")); } else { ModelState.AddModelError(string.Empty, await TranslationsService.TranslateAsync("Ongeldige login poging.")); return(Page()); } }
public async Task <IActionResult> SignInAsync([FromBody] FrontUser frontUser) { var user = await userRepository.FindByNameAsync(frontUser.UserName); if (user == null) { return(BadRequest(new { isAuthorized = false, reason = "Wrong login or password" })); } var signInResult = await signInService.PasswordSignInAsync(user, frontUser.Password, true, true); if (signInResult.Succeeded) { var tokenHandler = new JwtSecurityTokenHandler(); var key = Encoding.ASCII.GetBytes(appSettings.Secret); var tokenDescriptor = new SecurityTokenDescriptor { Subject = new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, user.Id) }), Expires = DateTime.UtcNow.AddHours(1), SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature) }; var token = tokenHandler.CreateToken(tokenDescriptor); var tokenString = tokenHandler.WriteToken(token); return(new OkObjectResult(new { isAuthorized = signInResult.Succeeded, token = tokenString })); } return(new OkObjectResult(new { isAuthorized = false })); }
public async Task <IActionResult> LoginAccountAsync([FromBody] LoginAccountRequest request) { var context = await _interactionService.GetAuthorizationContextAsync(request.ReturnUrl); if (context != null) { var user = await _userService.FindByEmailAsync(request.Email); if (user != null) { var result = await _signInService.PasswordSignInAsync( user, request.Password, request.RememberMe, true); if (result.Succeeded) { await _eventService.RaiseAsync(new UserLoginSuccessEvent(user.UserName, user.Id.ToString(), user.UserName)); return(this.Ok(request.ReturnUrl)); } var errorMessage = AccountErrorDescriber.PasswordInvalid(); await _eventService.RaiseAsync(new UserLoginFailureEvent(user.UserName, errorMessage)); ModelState.AddModelError(DomainValidationError.FailedPreconditionPropertyName, errorMessage); return(this.BadRequest(new ValidationProblemDetails(ModelState))); } } return(this.Unauthorized()); }
public async Task <IActionResult> Login(LoginViewModel model, string returnUrl = null) { ViewData["ReturnUrl"] = returnUrl; if (ModelState.IsValid) { // This doesn't count login failures towards account lockout // To enable password failures to trigger account lockout, set lockoutOnFailure: true bool isApprovedUser = _users.IsApprovedUser(model.Username); //DATA_MODELS.User user = _users.GetUserByName(model.Username).ProjectTo<DATA_MODELS.User>().FirstOrDefault(); DATA_MODELS.User user = _users .GetUserByName(model.Username) .Select(u => new DATA_MODELS.User { Id = u.Id, UserName = u.UserName, Name = u.Name, Email = u.Email, IsApproved = u.IsApproved }) .FirstOrDefault(); if (user == null) { ModelState.AddModelError(string.Empty, "No such user exists."); return(View(model)); } if (isApprovedUser == false) { ModelState.AddModelError(string.Empty, "You must wait to be approved by administrator."); return(View(model)); } var result = await _signInManager.PasswordSignInAsync(model.Username, model.Password, lockoutOnFailure : false); if (result.Succeeded && isApprovedUser) { _logger.LogInformation("User logged in."); return(RedirectToLocal(returnUrl)); } if (result.RequiresTwoFactor) { return(RedirectToAction(nameof(LoginWith2fa), new { returnUrl, model.RememberMe })); } if (result.IsLockedOut) { _logger.LogWarning("User account locked out."); return(RedirectToAction(nameof(Lockout))); } else { ModelState.AddModelError(string.Empty, "Invalid login attempt."); return(View(model)); } } // If we got this far, something failed, redisplay form return(View(model)); }
public virtual async Task <ActionResult> Login(LoginViewModel model, string returnUrl) { if (_userService.CheckIsUserBanned(model.UserName)) { this.AddErrors("UserName", "حساب کاربری شما مسدود شده است"); return(View(model)); } if (!ModelState.IsValid) { return(View(model)); } const string emailRegPattern = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"; var user = Regex.IsMatch(model.UserName, emailRegPattern) ? await _userService.FindByEmailAsync(model.UserName, model.Password) : await _userService.FindAsync(model.UserName, model.Password); if (user != null) { await _userService.UpdateSecurityStampAsync(user.Id); } if (user == null) { this.AddErrors("UserName", "کاربر مورد نظر یافت نشد"); return(View(model)); } var result = await _signInManager.PasswordSignInAsync (user.UserName, model.Password, model.RememberMe, shouldLockout : true); switch (result) { case SignInStatus.Success: return(RedirectToLocal(returnUrl)); case SignInStatus.LockedOut: this.AddErrors("UserName", $"دقیقه دوباره امتحان کنید {_userService.DefaultAccountLockoutTimeSpan} حساب شما قفل شد ! لطفا بعد از "); return(View(model)); case SignInStatus.Failure: this.AddErrors("UserName", "نام کاربری یا کلمه عبور صحیح نمی باشد"); this.AddErrors("Password", "نام کاربری یا کلمه عبور صحیح نمی باشد"); return(View(model)); default: this.AddErrors("UserName", "در این لحظه امکان ورود به سابت وجود ندارد . مراتب را با مسئولان سایت در میان بگذارید" ); return(View(model)); } }
public async Task <IActionResult> OnPostAsync(string returnUrl = null) { returnUrl ??= Url.Content("~/"); var result = await _signInService.PasswordSignInAsync(Input.Username, Input.Password, Input.RememberMe, lockoutOnFailure : true); if (result.Succeeded) { return(LocalRedirect(returnUrl)); } ModelState.AddModelError(string.Empty, await TranslationsService.TranslateAsync("Ongeldige login poging.")); return(await OnGetAsync()); }
public async Task <ActionResult> Login(SignInViewModel model) { var result = await _signInService.PasswordSignInAsync(model.UserName, model.Password); if (!result.Succeed) { foreach (var error in result.SignInErrors) { ModelState.AddModelError("", error.Description); return(View(model)); } } DoLogin(model); return(Redirect("/")); }
public async Task <IActionResult> Login([FromBody] LoginViewModel model, string returnUrl = null) { if (!ModelState.IsValid) { var err = ModelState.ToBadRequestErrorModel(); return(BadRequest(ModelState.ToBadRequestErrorModel())); } bool isApprovedUser = _users.IsApprovedUser(model.Username); DATA_MODELS.User user = _users .GetUserByName(model.Username) .Select(u => new DATA_MODELS.User { Id = u.Id, UserName = u.UserName, Name = u.Name, Email = u.Email, IsApproved = u.IsApproved }) .FirstOrDefault(); if (user == null) { ModelState.AddModelError(string.Empty, "No such user exists."); return(BadRequest(ModelState.ToBadRequestErrorModel())); } if (isApprovedUser == false) { ModelState.AddModelError(string.Empty, "You must wait to be approved by administrator."); return(StatusCode(401, ModelState.ToUnauthorizedErrorModel())); } var result = await _signInManager.PasswordSignInAsync(model.Username, model.Password, lockoutOnFailure : false); if (result.Succeeded && isApprovedUser) { _logger.LogInformation("User logged in."); return(Ok(user)); } else { ModelState.AddModelError(string.Empty, "Invalid login attempt."); return(BadRequest(ModelState.ToBadRequestErrorModel())); } }