public async Task <ClaimsPrincipal> GetClaimsPrincipalAsync(string apiKey) { if (_memoryCache.TryGetValue(apiKey, CacheType.ApiKey, out ClaimsPrincipal principals)) { return(principals); } principals = await _innerApiKeyManager.GetClaimsPrincipalAsync(apiKey); if (principals != null) { _memoryCache.Set(apiKey, CacheType.ApiKey, principals); } return(principals); }
protected override async Task <AuthenticateResult> HandleAuthenticateAsync() { string authHeader = Context.Request.Headers["Authorization"]; if (authHeader != null && authHeader.StartsWith(Options.HeaderScheme)) { // Extract api key string apiKey = authHeader.Substring(Options.HeaderScheme.Length).Trim(); var principals = await _apiKeyManager.GetClaimsPrincipalAsync(apiKey); if (principals != null) { return(AuthenticateResult.Success( new AuthenticationTicket(principals, Options.HeaderScheme))); } return(AuthenticateResult.Fail("Bad username or password")); } return(AuthenticateResult.NoResult()); }