protected async Task <AuthenticateResult> HandleAuthenticateAsync(string key) { if (string.IsNullOrWhiteSpace(key)) { return(AuthenticateResult.Fail("Invalid ApiKey authentication header.")); } // Validate key by using the implementation of IApiKeyValidationService. var validatedKey = await _apiKeyValidationService.ProvideAsync(key).ConfigureAwait(false); if (validatedKey == null) { return(AuthenticateResult.Fail("Invalid API Key provided.")); } // Create 'AuthenticationTicket' and return as success if the above validation was successful. var claims = new List <Claim>(); if (validatedKey.Claims != null) { claims.AddRange(validatedKey.Claims); } if (!string.IsNullOrWhiteSpace(validatedKey.OwnerName) && !claims.Exists(c => c.Type.Equals(ClaimTypes.Name, StringComparison.OrdinalIgnoreCase))) { claims.Add(new Claim(ClaimTypes.Name, validatedKey.OwnerName)); } var identity = new ClaimsIdentity(claims, Scheme.Name); var principal = new ClaimsPrincipal(identity); var ticket = new AuthenticationTicket(principal, Scheme.Name); return(AuthenticateResult.Success(ticket)); }