Exemplo n.º 1
0
        Task OnApiKeyValidated(ApiKeyValidatedContext context)
        {
            if (context.ApiKey == configuration.GetSection("ApiKeys")["DefaultKey"])
            {
                context.Success();
            }

            return(Task.CompletedTask);
        }
Exemplo n.º 2
0
        public void Validacia1(ApiKeyValidatedContext context)
        {
            // foreach (var kluc in this.databaza.ApiKeys) {

            // if (context.ApiKey == kluc.Key)
            // {
            context.Principal = new ClaimsPrincipal();

            context.Success();
            // }
            // else if (context.ApiKey == "789")
            // {
            throw new NotSupportedException("You must upgrade.");
            // }
            //}
        }
        protected override async Task <AuthenticateResult> HandleAuthenticateAsync()
        {
            try
            {
                var messageReceivedContext = new MessageReceivedContext(this.Context, this.Scheme, this.Options);

                await this.Events.MessageReceived(messageReceivedContext);

                string apiKey = messageReceivedContext.ApiKey;

                if (string.IsNullOrEmpty(apiKey))
                {
                    string header = this.Request.Headers[this.Options.Header];

                    if (string.IsNullOrEmpty(header))
                    {
                        this.Logger.ApiKeyValidationFailed();

                        return(AuthenticateResult.NoResult());
                    }

                    if (header.StartsWith(this.Options.HeaderKey, StringComparison.OrdinalIgnoreCase))
                    {
                        apiKey = header.Substring(this.Options.HeaderKey.Length).Trim();

                        var validateApiKeyContext = new ApiKeyValidatedContext(this.Context, this.Scheme, this.Options)
                        {
                            ApiKey    = apiKey,
                            Principal = new ClaimsPrincipal()
                        };

                        await this.Events.ApiKeyValidated(validateApiKeyContext);

                        if (validateApiKeyContext.Result != null)
                        {
                            this.Logger.ApiKeyValidationSucceeded();

                            return(validateApiKeyContext.Result);
                        }
                    }
                }

                this.Logger.ApiKeyValidationFailed();

                return(AuthenticateResult.NoResult());
            }
            catch (Exception ex)
            {
                this.Logger.ErrorProcessingMessage(ex);

                var authenticationFailedContext = new AuthenticationFailedContext(this.Context, this.Scheme, this.Options)
                {
                    Exception = ex
                };

                await this.Events.AuthenticationFailed(authenticationFailedContext);

                if (authenticationFailedContext.Result != null)
                {
                    return(authenticationFailedContext.Result);
                }

                throw;
            }
        }