protected override async Task <AuthenticateResult> HandleAuthenticateAsync() { string username = null; try { var authHeader = AuthenticationHeaderValue.Parse(Request.Headers["Authorization"]); var credentials = Encoding.UTF8.GetString(Convert.FromBase64String(authHeader.Parameter)).Split(':'); username = credentials.FirstOrDefault(); var password = credentials.LastOrDefault(); var response = await _siteMercadoService.ValidateCredentials(username, password); var authResult = JsonConvert.DeserializeObject <AuthResult>(response); if (!authResult.Success) { throw new ArgumentException(authResult.Error); } } catch (Exception ex) { return(AuthenticateResult.Fail($"Authentication failed: {ex.Message}")); } var claims = new[] { new Claim(ClaimTypes.Name, username) }; var identity = new ClaimsIdentity(claims, Scheme.Name); var principal = new ClaimsPrincipal(identity); var ticket = new AuthenticationTicket(principal, Scheme.Name); return(AuthenticateResult.Success(ticket)); }
public IActionResult Authenticate([FromBody] AuthenticateModel model) { //#if (DEBUG) // model.Username = "******"; // model.Password = "******"; //#endif var response = _service.ValidateCredentials(model.Username, model.Password).Result; var authResult = JsonConvert.DeserializeObject <AuthResult>(response); if (!authResult.Success) { return(BadRequest(new { Message = authResult.Error })); } return(Ok(authResult)); }