コード例 #1
0
        public void ReturnUserClaimsDTOForAuthenticatedUser()
        {
            // GIVEN a UserDTO containing a user's e-mail and password
            UserDTO user = new UserDTO
            {
                EmailAddress = "*****@*****.**",
                Password     = "******"
            };

            // WHEN the user is correctly authenticated
            // AND the user's login information is returned from the database
            TrainerCredentials mockTrainerCredentials = new TrainerCredentials
            {
                EmailAddress = "*****@*****.**",
                Hash         = PASSWORD1234_HASH,
                Salt         = PASSWORD1234_SALT
            };

            accountContextMock.Setup(a => a.TrainerCredentials.Find(user.EmailAddress)).Returns(mockTrainerCredentials);

            Trainer mockTrainer = new Trainer
            {
                EmailAddress = "*****@*****.**",
                FirstName    = "Test",
                LastName     = "User"
            };

            accountContextMock.Setup(a => a.Trainer.Find(mockTrainer.EmailAddress)).Returns(mockTrainer);

            configMock.Setup(c => c.Value.JwtKey).Returns(JWT_KEY);

            UserClaimsDTO userClaims = accountServices.AuthorizeTrainer(user);

            // THEN return a UserClaimsDTO containing an e-mail claim with the
            // user's e-mail, a name claim with the user's full name, a role claim
            // of trainer, and a TrainerId claim with the trainer's ID
            List <Claim> claims = new List <Claim> {
                new Claim(ClaimTypes.Email, "*****@*****.**"),
                new Claim(ClaimTypes.Name, "Test User"),
                new Claim(ClaimTypes.Role, UserRole.TRAINER.ToString())
            };

            for (int i = 0; i < claims.Count; i++)
            {
                Assert.Equal(claims[i].GetType(), userClaims.Claims[i].GetType());
                Assert.Equal(claims[i].Value, userClaims.Claims[i].Value);
            }

            // AND an encrypted Token
            var handler       = new JwtSecurityTokenHandler();
            var decodedClaims = handler.ReadToken(userClaims.Token) as JwtSecurityToken;

            Assert.NotNull(decodedClaims);
        }
        public async Task <ApiResult> CreateUserClaims(string userName, UserClaimsDTO model)
        {
            var user = await userManager.FindByNameAsync(userName);

            var result = await userManager.AddClaimsAsync(user, model.Claims.Select(p => new Claim(p.type, p.value)));

            if (result.Succeeded)
            {
                return(Ok());
            }

            return(BadRequest());
        }
コード例 #3
0
        public void ReturnUserClaimsDTOWithClaimsAndToken()
        {
            // GIVEN a Claims object contatining claims
            List <Claim> expectedClaims = new List <Claim>
            {
                new Claim(ClaimTypes.Email, "*****@*****.**"),
                new Claim(ClaimTypes.Name, "Test User"),
                new Claim(ClaimTypes.Role, UserRole.TRAINER.ToString())
            };
            Claims claims = new Claims(expectedClaims);

            // WHEN a UserClaimsDTO object is being generated
            UserClaimsDTO userClaims = claims.GenerateUserClaimsDTO(JWT_KEY);

            // THEN a UserClaimsDTO object with claims and a token is returned
            Assert.Equal(expectedClaims, userClaims.Claims);
            Assert.NotNull(userClaims.Token);
        }
コード例 #4
0
        public async Task ReturnSuccessWithClaimsAndCookieToken()
        {
            UserDTO user = new UserDTO
            {
                EmailAddress = "*****@*****.**",
                Password     = "******"
            };
            string jsonInString = JsonConvert.SerializeObject(user);

            HttpResponseMessage response = await httpClient.PostAsync("/api/account/login", new StringContent(jsonInString, Encoding.UTF8, "application/json"));

            response.EnsureSuccessStatusCode();

            string stringResponse = await response.Content.ReadAsStringAsync();

            UserClaimsDTO userClaims = JsonConvert.DeserializeObject <UserClaimsDTO>(stringResponse, new UserClaimsDtoJsonConverter());

            Assert.Equal(3, userClaims.Claims.Count);
            Assert.NotNull(userClaims.Token);
        }