/// <summary> /// Handle Authenticate Async /// </summary> /// <returns>Task of AuthenticateResult</returns> protected override async Task <AuthenticateResult> HandleAuthenticateAsync() { if (!Request.Headers.ContainsKey("Authorization")) { return(AuthenticateResult.Fail("Authorization Header not found")); } Register register; try { var authHeader = AuthenticationHeaderValue.Parse(Request.Headers["Authorization"]); byte[] credentialBytes = Convert.FromBase64String(authHeader.Parameter); string[] credentials = Encoding.UTF8.GetString(credentialBytes).Split(':'); string login = credentials[0]; string password = credentials[1]; register = await registerService.Auth(login, password); } catch (Exception) { return(AuthenticateResult.Fail("Authorization Header isn't valid")); } if (register == null) { return(AuthenticateResult.Fail("Username or Password isn't valid")); } var claims = new Claim[] { new Claim(ClaimTypes.NameIdentifier, register.Login), new Claim(ClaimTypes.Name, register.Password), }; var identity = new ClaimsIdentity(claims, Scheme.Name); var principal = new ClaimsPrincipal(identity); var ticket = new AuthenticationTicket(principal, Scheme.Name); return(AuthenticateResult.Success(ticket)); }