public void JwtSecurityProviderCreated()
        {
            IServiceProvider sp = StartUp.ServiceProvider;

            IJwtSecurityProvider        jwtSecurityProvider = sp.GetService <IJwtSecurityProvider>();
            IModelTokenGenerator <User> TokenGenerator      = GetTokenGenerator();
            string token = jwtSecurityProvider.WriteToken <User>(TokenGenerator);
            ReadOnlyCollection <Claim> claims = jwtSecurityProvider.GetClaimsCollection(token);

            Assert.IsNotNull(token);
            Assert.IsNotNull(claims.GetKey(UserKey));
            Assert.IsNotNull(claims.GetKey(ClaimTypes.Role));

            //string role = claims.GetKey(ClaimTypes.Role);

            //IEncryptionProvider encryptionProvider = sp.GetService<IEncryptionProvider>();
            //string text = "I am sergio";
            //string encrypted = encryptionProvider.Encrypt(text);
            //string decrypted = encryptionProvider.Decrypt(encrypted);

            //DecryptProvider decryptService = new DecryptProvider();
            //EncryptionKeyProvider encryptProvider = new EncryptionKeyProvider(decryptService);
            //string enc = encryptProvider.Encrypt(text);
            //string key = encryptProvider.EncryiptionKey;
            //var value = decryptService.Decrypt(enc, key);

            //Assert.IsTrue(text == decrypted);



            //Assert.IsNotNull(token);
        }
        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);
        }
Example #3
0
        public ReadOnlyCollection <Claim> GetClaimsCollection(string encryptedToken)
        {
            string decryptedToken = Decrypt(encryptedToken);

            return(jwtSecurityProvider.GetClaimsCollection(decryptedToken));
        }