public bool HandleAuthenticate(HttpRequest request) { if (!request.Headers.ContainsKey(AUTH_HEADER_NAME)) { return(false); } User user = null; try { var authHeader = request.Headers[AUTH_HEADER_NAME]; var authHeaderValue = authHeader[0]; if (!authHeaderValue.StartsWith(BASIC_AUTH_NAME)) { return(false); } var encodedValue = authHeaderValue.Substring(BASIC_AUTH_NAME.Length); var credentialBytes = Convert.FromBase64String(encodedValue); var credentials = Encoding.UTF8.GetString(credentialBytes).Split(new[] { ':' }, 2); var username = credentials[0]; var password = credentials[1]; user = _usersRepo.Authenticate(username, password); } catch { return(false); } return(true); }
public async Task <IActionResult> Authenticate([FromBody] AuthenticateModel model) { var user = await _users.Authenticate(model.Username, model.Password); if (user == null) { return(BadRequest(new { message = "Username or password is incorrect" })); } return(Ok(user)); }
protected override Task <AuthenticateResult> HandleAuthenticateAsync() { User user = null; try { string authHeader = Request.Headers["Authorization"]; Console.WriteLine(authHeader); if (authHeader != null && authHeader.StartsWith("Basic")) { string encodedUsernamePassword = authHeader.Substring("Basic ".Length).Trim(); Encoding encoding = Encoding.GetEncoding("iso-8859-1"); string usernamePassword = encoding.GetString(Convert.FromBase64String(encodedUsernamePassword)); int seperatorIndex = usernamePassword.IndexOf(':'); var header_username = usernamePassword.Substring(0, seperatorIndex); var header_password = usernamePassword.Substring(seperatorIndex + 1); user = _userService.Authenticate(header_username, header_password); } else { return(Task.FromResult(AuthenticateResult.Fail("Failed to retrieve Authorization from header"))); } if (user == null) { return(Task.FromResult(AuthenticateResult.Fail("Failed to authenticate user: credentials don't match any user"))); } var claims = new[] { new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()), new Claim(ClaimTypes.Name, user.Username), }; var identity = new ClaimsIdentity(claims, Scheme.Name); var principal = new ClaimsPrincipal(identity); var ticket = new AuthenticationTicket(principal, Scheme.Name); return(Task.FromResult(AuthenticateResult.Success(ticket))); } catch (Exception e) { return(Task.FromResult(AuthenticateResult.Fail($"Exception thrown retrieving user: {e.Message}"))); } }
public async Task <AuthenticateResponse> Authenticate(AuthenticateRequestDto authenticationRequestDto) { if (authenticationRequestDto == null) { throw new ArgumentNullException(nameof(authenticationRequestDto)); } var user = await _usersRepo.Authenticate(authenticationRequestDto.Username, authenticationRequestDto.Password); return(user == null ? null : new AuthenticateResponse { Token = GenerateJwtToken(user) }); }
protected override async Task <AuthenticateResult> HandleAuthenticateAsync() { if (!Request.Headers.ContainsKey("Authorization")) { return(AuthenticateResult.Fail("Missing Authorization Header")); } User user = null; 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 = await _userService.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.Username) }; var identity = new ClaimsIdentity(claims, Scheme.Name); var principal = new ClaimsPrincipal(identity); var ticket = new AuthenticationTicket(principal, Scheme.Name); return(AuthenticateResult.Success(ticket)); }