public void GetAdminAsSuperFail()
        {
            var model = new MakeTokenViewModel()
            {
                Id       = 1,
                UserName = "******",
                Role     = "super"
            };
            var signingKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(
                                                          _mockConfiguration.Object["Jwt:SigningKey"]));
            int    experyInMinutes = Convert.ToInt32(_mockConfiguration.Object["Jwt:ExperyInMinutes"]);
            string site            = _mockConfiguration.Object["Jwt:Site"];

            JwtSecurityToken token = makeToken(model, signingKey, experyInMinutes, site);

            var encodedJwt = new JwtSecurityTokenHandler().WriteToken(token);

            var user = new ClaimsPrincipal(new ClaimsIdentity(makeClaimList(model)));


            var controller = new ValuesController();

            controller.ControllerContext             = new ControllerContext();
            controller.ControllerContext.HttpContext = new DefaultHttpContext()
            {
                User = user
            };
            controller.ControllerContext.HttpContext.Request.Headers["Authorization"] = $"Bearer {encodedJwt}";

            var result = controller.GetAdmin();

            ((ObjectResult)result).StatusCode.Should().Be(200);

            System.Diagnostics.Debug.WriteLine(result.ToString());
        }
 private static IEnumerable <Claim> makeClaimList(MakeTokenViewModel model)
 {
     return(new[]
     {
         new Claim("name", model.UserName),
         new Claim("id", model.Id.ToString()),
         new Claim("role", model.Role)
     });
 }
        private static JwtSecurityToken makeToken(MakeTokenViewModel model, SymmetricSecurityKey signingKey, int experyInMinutes, string site)
        {
            // Create claims for any data I want to embed into the JWT
            IEnumerable <Claim> claim = makeClaimList(model);

            // Create the sighed token
            var token = new JwtSecurityToken(
                issuer: site,
                audience: site,
                expires: DateTime.UtcNow.AddMinutes(experyInMinutes),
                signingCredentials: new SigningCredentials(signingKey, SecurityAlgorithms.HmacSha256),
                claims: claim
                );

            return(token);
        }
Exemplo n.º 4
0
        public ActionResult Login([FromBody] MakeTokenViewModel model)
        {
            // Simulate login or other failure:
            if (model.Fail)
            {
                return(BadRequest("JWT Creation Failure"));
            }

            // Get configuration data
            var signingKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(
                                                          _configuration["Jwt:SigningKey"]));
            int    experyInMinutes = Convert.ToInt32(_configuration["Jwt:ExperyInMinutes"]);
            string site            = _configuration["Jwt:Site"];

            JwtSecurityToken token = makeToken(model, signingKey, experyInMinutes, site);

            // send it out as 200
            return(Ok(new JwtSecurityTokenHandler().WriteToken(token)));
        }
        public void LoginFail()
        {
            // Arrange
            MakeTokenViewModel model = new MakeTokenViewModel()
            {
                Id       = 7,
                UserName = "******",
                Role     = "admin",
                Fail     = true
            };
            var controller = new JwtController(_mockConfiguration.Object);

            // Act
            var result = controller.Login(model);

            // Assert
            ((ObjectResult)result).StatusCode.Should().Be(400);

            Console.WriteLine(result);
        }
Exemplo n.º 6
0
        private static JwtSecurityToken makeToken(MakeTokenViewModel model, SymmetricSecurityKey signingKey, int experyInMinutes, string site)
        {
            // Create claims for any data I want to embed into the JWT
            var claim = new[]
            {
                new Claim("name", model.UserName),
                new Claim("id", model.Id.ToString()),
                new Claim("role", model.Role)
            };

            // Create the sighed token
            var token = new JwtSecurityToken(
                issuer: site,
                audience: site,
                expires: DateTime.UtcNow.AddMinutes(experyInMinutes),
                signingCredentials: new SigningCredentials(signingKey, SecurityAlgorithms.HmacSha256),
                claims: claim
                );

            return(token);
        }