Ejemplo n.º 1
0
        public void OnAuthentication(AuthenticationContext filterContext)
        {
            var request = filterContext.HttpContext.Request;

            try
            {
                // Parse the Authentication header
                var header = AuthenticationHeaderValue.Parse(request.Headers["Authorization"]);
                if (header == null || header.Scheme != "Bearer" || string.IsNullOrWhiteSpace(header.Parameter))
                {
                    return;
                }

                // Extract the principal from the header
                var principal = Context.Services.TokenService.ReadToken(header.Parameter); //TODO: Check this validate
                if (principal == null)
                {
                    return;
                }

                // Validate the principal
                if (!PrincipalHelper.ValidatePrincipal(principal, Realm, Context.Services.UserService))
                {
                    return;
                }

                // Set the current principal
                filterContext.Principal = principal;
            }
            catch (FormatException ex)
            {
                return;
            }
        }
Ejemplo n.º 2
0
        public async Task AuthenticateAsync(HttpAuthenticationContext context, CancellationToken cancellationToken)
        {
            var request = context.Request;

            // Parse the Authentication header
            var header = request.Headers.Authorization;

            if (header == null || header.Scheme != "Bearer" || string.IsNullOrEmpty(header.Parameter))
            {
                return;
            }

            // Extract the principal from the header
            var principal = Context.Services.TokenService.ReadToken(header.Parameter); //TODO: Check this validate

            if (principal == null)
            {
                return;
            }

            // Validate the principal
            if (!PrincipalHelper.ValidatePrincipal(principal, Realm, Context.Services.UserService))
            {
                return;
            }

            // Set the current principal
            context.Principal = principal;
        }