public IActionResult Authenticate(User user)
        {
            int checkLogin = bl_usersManager.Login(user.UserName, user.UserPwd);

            if (checkLogin == 1)
            {
                return(BadRequest(new { message = "Connect databse failed" }));
            }
            else if (checkLogin == 2)
            {
                return(BadRequest(new { message = "Username or password is incorrect" }));
            }

            var tokenHandler    = new JwtSecurityTokenHandler();
            var key             = Encoding.ASCII.GetBytes("daskjdhsjkdhsajkdhjdhasjkdhasmncxnmcbheaggdhjawdg");
            var tokenDescriptor = new SecurityTokenDescriptor
            {
                Subject = new ClaimsIdentity(new Claim[]
                {
                    new Claim(ClaimTypes.Name, user.UserName)
                }),
                Expires            = DateTime.UtcNow.AddSeconds(1),
                SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
            };
            var token       = tokenHandler.CreateToken(tokenDescriptor);
            var tokenString = new JwtSecurityTokenHandler().WriteToken(token);

            Console.WriteLine(DateTime.Now.AddSeconds(1));
            return(Ok(new { token = token }));
        }
Пример #2
0
        public void BlLoginTest()
        {
            BL.UsersManager bl_usersManager = new BL.UsersManager();
            int             checkLogin      = bl_usersManager.Login("tungpt260794", "123456");

            Console.WriteLine("checkLogin = {0}", checkLogin);
            Assert.True(checkLogin == 0);
        }
Пример #3
0
        public IActionResult Index([Bind("UserName, UserPwd")] LoginViewModel loginViewModel)
        {
            int checkLogin = bl_usersManager.Login(loginViewModel.UserName, loginViewModel.UserPwd);

            if (checkLogin == 0)
            {
                return(RedirectToAction("Index", "Home", new { id = loginViewModel.UserName }));
            }

            return(View());
        }