public IActionResult Login(LoginViewModel model) { if (!ModelState.IsValid) { return(View(model)); } if (_httpContextAccessor.HttpContext.Session.GetString("Captcha_login") != model.Captcha) { _httpContextAccessor.HttpContext.Session.Remove("Captcha_login"); ModelState.AddModelError("Captcha", "کد امنیتی نادرست میباشد"); return(View(model)); } var _user = _userService.Authenticate(model.Username, model.Password); if (_user == null) { ModelState.AddModelError("", "مشخصات نامعتبر"); _httpContextAccessor.HttpContext.Session.Remove("Captcha_login"); return(View(model)); } else { _httpContextAccessor.HttpContext.Session.Remove("Captcha_login"); var _token = _tokenHelper.CreateUserToken(_user).Token; _tokenHelper.SetCookie("_authorization", _token); return(RedirectToAction(actionName: "BaseMenu", controllerName: "home")); } }
public async Task <string> LogIn(LoginRequest request) { var user = await dbContext.Users.FirstOrDefaultAsync(u => u.Email == request.Email); var result = await signInManager.PasswordSignInAsync(user?.UserName, request.Password, request.RememberMe, false); if (result.Succeeded) { var token = tokenHelper.CreateUserToken(configuration.GetSecurityKey(), DateTime.Now.AddMinutes(30), request.Email); return(token); } throw new BadRequestException(); }
public object Login(LoginViewModel loginViewModel) { if (!ModelState.IsValid) { return(Request.CreateResponse(HttpStatusCode.BadRequest, new BaseResponse(ResponseStatus.ValidationError.ToString(), ModelState.Values.ToList()[0].Errors[0].ErrorMessage))); } if (!_accountLogic.IsUserExist(loginViewModel.Username, HashManagement.Md5Hash(loginViewModel.Password))) { return(Request.CreateResponse(HttpStatusCode.Unauthorized, new BaseResponse(ResponseStatus.ValidationError.ToString(), ResponseMessagesModel.UsernameOrPassIsWrong))); } UserData userData = _accountLogic.GetUser(loginViewModel.Username); return(Request.CreateResponse(HttpStatusCode.OK, new BaseResponse(ResponseStatus.Success.ToString(), ResponseMessagesModel.Success, _tokenHelper.CreateUserToken(userData)))); }