protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, AuthorizationTokenRequirement requirement)
        {
            Microsoft.AspNetCore.Mvc.Filters.AuthorizationFilterContext resource          = context.Resource as Microsoft.AspNetCore.Mvc.Filters.AuthorizationFilterContext;
            Microsoft.AspNetCore.Http.DefaultHttpContext          httpContext             = resource.HttpContext as Microsoft.AspNetCore.Http.DefaultHttpContext;
            Microsoft.AspNetCore.Http.Internal.DefaultHttpRequest request                 = httpContext.Request as Microsoft.AspNetCore.Http.Internal.DefaultHttpRequest;
            Microsoft.AspNetCore.Server.Kestrel.Internal.Http.FrameRequestHeaders headers = request.Headers as Microsoft.AspNetCore.Server.Kestrel.Internal.Http.FrameRequestHeaders;

            string AuthorizationToken = headers.HeaderAuthorization;

            if (String.IsNullOrEmpty(AuthorizationToken))
            {
                context.Fail();
            }
            else
            {
                string authvalue    = AuthorizationToken.Replace("Bearer ", "");
                bool   isTokenValid = tokenProvider.IsTokenValid(authvalue);
                if (isTokenValid)
                {
                    ReadOnlyCollection <Claim> claims = tokenProvider.GetClaimsCollection(authvalue);
                    currentAuthenticationContext.setCurrentUser(claims.GetKey(ClaimKeys.USER_ID));
                    currentAuthenticationContext.setCurrentRoleId(claims.GetKey(ClaimKeys.ROLE));
                    context.Succeed(requirement);
                }
                else
                {
                    context.Fail();
                }
            }


            return(Task.CompletedTask);
        }
Exemplo n.º 2
0
        public bool IsTokenValid(string encryptedToken)
        {
            string decryptedToken = Decrypt(encryptedToken);

            return(jwtSecurityProvider.IsTokenValid(decryptedToken));
        }