Exemplo n.º 1
0
        protected override Task <AuthenticateResult> HandleAuthenticateAsync()
        {
            if (!Request.Headers.TryGetValue(ApiKeyHeaderName, out var apiKeyHeaderValues))
            {
                return(Task.FromResult(AuthenticateResult.NoResult()));
            }

            var providedApiKey = apiKeyHeaderValues.FirstOrDefault();

            if (apiKeyHeaderValues.Count == 0 || string.IsNullOrWhiteSpace(providedApiKey))
            {
                return(Task.FromResult(AuthenticateResult.NoResult()));
            }

            var authResult = _authenticationService.Authenticate(providedApiKey);

            if (authResult)
            {
                var identity   = new ClaimsIdentity(Options.AuthenticationType);
                var identities = new List <ClaimsIdentity> {
                    identity
                };
                var principal = new ClaimsPrincipal(identities);
                var ticket    = new AuthenticationTicket(principal, Options.Scheme);

                return(Task.FromResult(AuthenticateResult.Success(ticket)));
            }

            return(Task.FromResult(AuthenticateResult.Fail("Bad API Key")));
        }
        public async Task Invoke(HttpContext context)
        {
            var login = context.Request.Headers["login"];
            var pwd   = context.Request.Headers["password"];

            var isValid = _authenticationService.Authenticate(login, pwd);

            if (!isValid)
            {
                context.Response.StatusCode = 401;
                await context.Response.WriteAsync("Aqui não queridinho!");
            }

            await _next.Invoke(context);
        }
Exemplo n.º 3
0
        protected override async Task <AuthenticateResult> HandleAuthenticateAsync()
        {
            if (!Request.Headers.ContainsKey("Authorization"))
            {
                return(AuthenticateResult.Fail("Missing Authorization Header"));
            }

            string authenticationToken = GetBasicAuthenticationTokenFromRequest(Request);

            if (string.IsNullOrWhiteSpace(authenticationToken))
            {
                return(AuthenticateResult.Fail("Invalid Authorization Header"));
            }

            try
            {
                string login    = GetLoginFromAuthenticationToken(authenticationToken);
                string password = GetPasswordFromAuthenticationToken(authenticationToken);

                bool isValidAuthToken = _authenticationService.Authenticate(login, password);
                if (!isValidAuthToken)
                {
                    return(AuthenticateResult.Fail("Unregistered Authorization Header"));
                }
                else
                {
                    Claim[] claims = new[] {
                        new Claim(ClaimTypes.Name, login),
                    };

                    ClaimsIdentity       identity  = new ClaimsIdentity(claims, Scheme.Name);
                    ClaimsPrincipal      principal = new ClaimsPrincipal(identity);
                    AuthenticationTicket ticket    = new AuthenticationTicket(principal, Scheme.Name);

                    return(AuthenticateResult.Success(ticket));
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, "Error during authentication");
                return(AuthenticateResult.Fail(ex));
            }
        }
Exemplo n.º 4
0
 public IActionResult ValidateApiKey(ValidateApiKeyRequest request)
 {
     return(Ok(_authenticationService
               .Authenticate(request.ApiKey)));
 }