protected override async Task <AuthenticateResult> HandleAuthenticateAsync() { // Get Authorization header value if (!Request.Headers.TryGetValue(HeaderNames.Authorization, out var authorization)) { return(AuthenticateResult.Fail($"Cannot read {HeaderNames.Authorization} header.")); } var apiKey = authorization.First().Split(" ").Last(); var apiKeyItem = await apiKeyProvider.Get(apiKey); if (apiKeyItem == null) { return(AuthenticateResult.Fail("ApiKey not found")); } var identities = new List <ClaimsIdentity> { new ClaimsIdentity( new List <Claim> { new Claim(ClaimTypes.Name, apiKey, ClaimValueTypes.String), new Claim(ClaimTypes.NameIdentifier, apiKeyItem.UserId.ToString(), ClaimValueTypes.String), new Claim(ClaimTypes.UserData, apiKeyItem.ApiKeyId.ToString(), ClaimValueTypes.Integer), }, ApiKeyAuthOptions.DefaultScheme ) }; var ticket = new AuthenticationTicket(new ClaimsPrincipal(identities), Options.Scheme); return(AuthenticateResult.Success(ticket)); }