protected override async Task <AuthenticateResult> HandleAuthenticateAsync() { if (!Request.Headers.TryGetValue(ApiKeyHeaderName, out var apiKeyHeaderValues)) { return(AuthenticateResult.NoResult()); } Guid.TryParse(apiKeyHeaderValues.FirstOrDefault(), out var apiKey); if (apiKeyHeaderValues.Count == 0 || apiKey == Guid.Empty) { return(AuthenticateResult.NoResult()); } var existingApiKey = await _apiKeyService.FindApiKeyAsync(apiKey); if (existingApiKey == null) { return(AuthenticateResult.Fail("Invalid API Key provided.")); } var claims = new List <Claim> { new Claim(ClaimTypes.Name, existingApiKey.Owner) }; var identity = new ClaimsIdentity(claims, Options.AuthenticationType); var identities = new List <ClaimsIdentity> { identity }; var principal = new ClaimsPrincipal(identities); var ticket = new AuthenticationTicket(principal, Options.Scheme); return(AuthenticateResult.Success(ticket)); }