public void LoginTest()
        {
            Mock <IConfiguration> config = new Mock <IConfiguration>();

            config.Setup(p => p["Jwt:Key"]).Returns("MyCustomSecretKey");
            var controller = new TokenController(config.Object, usercontextmock.Object);
            var rentuser   = new RentingUser {
                UserId = "user1", Password = "******", FullName = "abc"
            };
            var auth = controller.Login(rentuser) as OkObjectResult;

            Assert.AreEqual(200, auth.StatusCode);
        }
Exemplo n.º 2
0
        public RentingUser AuthenticateUser(RentingUser login)
        {
            //RentingContext context = new RentingContext();
            //RentingUser usr = null;
            _log4net.Info("Validating the Vendor!");



            //Validate the User Credentials
            RentingUser usr = context.users.FirstOrDefault(c => c.UserId == login.UserId && c.Password == login.Password);


            return(usr);
        }
Exemplo n.º 3
0
        public string GenerateJSONWebToken(RentingUser userInfo)
        {
            _log4net.Info("Token Is Generated!");

            var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config["Jwt:Key"]));
            var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);


            var token = new JwtSecurityToken(
                issuer: _config["Jwt:Issuer"],
                audience: _config["Jwt:Issuer"],
                null,
                expires: DateTime.Now.AddMinutes(30),
                signingCredentials: credentials);



            return(new JwtSecurityTokenHandler().WriteToken(token));
        }
Exemplo n.º 4
0
        public IActionResult Login([FromBody] RentingUser login)
        {
            _log4net.Info("Login initiated!");
            IActionResult response = Unauthorized();
            //login.FullName = "user1";
            var user = AuthenticateUser(login);

            if (user == null)
            {
                return(NotFound());
            }
            else
            {
                var tokenString = GenerateJSONWebToken(user);
                response = Ok(new { token = tokenString });
            }

            return(response);
        }