public async Task <ActionResult> Login(LoginModel model) { await SetInitialDataAsync(); if (ModelState.IsValid) { UserDTO userDto = _mapper.Map <UserDTO>(model); ClaimsIdentity claim = await _userManagementService.Authenticate(userDto); if (claim == null) { ModelState.AddModelError("", "Login or Password is incorrect!"); } else { _authManager.SignOut(); _authManager.SignIn(new AuthenticationProperties { IsPersistent = true }, claim); string id = await _userDataService.GetIdByUserName(userDto.UserName); return(RedirectToAction("Index", "Home", new { area = "User", userId = id })); } } return(View(model)); }
public IActionResult Authenticate([FromBody] User user) { string token = _service.Authenticate(user); if (String.IsNullOrEmpty(token)) { return(new ObjectResult(token)); } return(new UnauthorizedObjectResult(new { message = "Erreur" })); }
public async Task <string> Authenticate([FromBody] AuthRequestModel authRequest) { try { return(await _userManagementService.Authenticate(authRequest.Email, authRequest.Password)); } catch (Exception e) { // send the exception details to the logger Console.WriteLine(e); } return("Unable to generate the Bearer token!"); }
protected override async Task <AuthenticateResult> HandleAuthenticateAsync() { if (!Request.Headers.ContainsKey("Authorization")) { return(AuthenticateResult.Fail("Missing Authorization Header")); } UserDto user; try { var authHeader = AuthenticationHeaderValue.Parse(Request.Headers["Authorization"]); var credentialBytes = Convert.FromBase64String(authHeader.Parameter); var credentials = Encoding.UTF8.GetString(credentialBytes).Split(new[] { ':' }, 2); var username = credentials[0]; var password = credentials[1]; user = _userManagementService.Authenticate(username, password); } catch { return(AuthenticateResult.Fail("Invalid Authorization Header")); } if (user == null) { return(AuthenticateResult.Fail("Invalid Username or Password")); } var claims = new[] { new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()), new Claim(ClaimTypes.Name, user.Name), }; var identity = new ClaimsIdentity(claims, Scheme.Name); var principal = new ClaimsPrincipal(identity); var ticket = new AuthenticationTicket(principal, Scheme.Name); return(AuthenticateResult.Success(ticket)); }