示例#1
0
        protected override async Task <AuthenticateResult> HandleAuthenticateAsync()
        {
            if (!Request.Query.ContainsKey(API_KEY_PARAM_NAME))
            {
                return(AuthenticateResult.NoResult());
            }
            var providedApiKey = Request.Query[API_KEY_PARAM_NAME];

            if (string.IsNullOrWhiteSpace(providedApiKey))
            {
                return(AuthenticateResult.NoResult());
            }

            if (await _apiKeyService.VerifyApiKey(providedApiKey))
            {
                var claims = new List <Claim>
                {
                    new Claim(ClaimTypes.AuthenticationMethod, Options.AuthenticationType)
                };
                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));
            }

            return(AuthenticateResult.Fail("Invalid API Key provided."));
        }