public async Task <IActionResult> SignIn(SignInVm data) { if (ModelState.IsValid) { var user = await _userManager.FindByNameAsync(data.UserName); if (user == null) { return(View()); } var result = await _signInManager.CheckPasswordSignInAsync(user, data.Password, false); if (!result.Succeeded) { return(View()); } await _signInManager.SignInAsync(user, true); return(RedirectToAction("Index", "Home")); } return(View()); }
[HttpPost("token")] //route prefix yerine direk içine. yani bura için api/token public async Task <ActionResult> GetToken(SignInVm data) { var user = await _userManager.FindByNameAsync(data.UserName); if (user == null) { return(BadRequest()); //bad req controller base den geliyor. } var result = await _signInManager.CheckPasswordSignInAsync(user, data.Password, false); //false lockout u devre dışı bırakmada if (!result.Succeeded) { return(BadRequest()); } //kullanıcımızsa ona token üretip verme prosedürü işlemeye başlar: var signingCredentials = new SigningCredentials(KeyProvider.SymmetricSecurityKey, SecurityAlgorithms.HmacSha256Signature); var token = new JwtSecurityToken( issuer: "system", //startupta belirlediğimiz audience: "reader", //startupta belirledğimiz expires: DateTime.Now.AddHours(1), signingCredentials: signingCredentials ); var tokenHandler = new JwtSecurityTokenHandler(); return(Ok(new { UserId = user.Id, Token = tokenHandler.WriteToken(token) })); }
public IActionResult SignIn(SignInVm vm) { if (!ModelState.IsValid) { return(View(vm)); } HttpContext.Session.SetString(UserNameKey, vm.UserName); return(RedirectToAction("Index")); }
public async Task <UserVm> SignIn(SignInVm model) { var user = await _userRepository.SignIn(_mapper.Map <SignInDto>(model)); if (user == null) { throw new BadRequestException("Invalid email or password"); } if (!user.IsActive) { throw new BadRequestException("User is not active, please activate user via email"); } return(_mapper.Map <UserVm>(user)); }
public async Task <IActionResult> SignIn([FromBody] SignInVm model) { var user = await _userService.SignIn(model); if (user == null) { throw new BadRequestException("Invalid username or password"); } return(this.Ok(new { Token = this.GenerateJwt(user), Email = user.Email, FirstName = user.FirstName, LastName = user.LastName, Id = user.Id })); }