Exemplo n.º 1
0
        public IActionResult LoginUser()
        {
            string login       = HttpContext.Request.Form["Login"];
            string password    = HttpContext.Request.Form["Password"];
            string SessionName = login;
            var    typUser     = 0;
            var    userid      = 0;

            typUser = UserDatabaseContext.Login(login, password, 1);
            userid  = UserDatabaseContext.Login(login, password, 2);

            HttpContext.Session.SetString(Appsettings.SESSIONLOGIN, login);
            HttpContext.Session.SetString(Appsettings.SESSIONTYPUSER, Convert.ToString(typUser));
            HttpContext.Session.SetString(Appsettings.SESSIONLOGINID, Convert.ToString(userid));

            var claims = new List <Claim>()
            {
                new Claim(ClaimTypes.Role, ((Models.User.UserType)typUser).ToString()),
            };

            var identity = new ClaimsIdentity(claims, "identity");

            var userPrincipal = new ClaimsPrincipal(new[] { identity });

            //-----------------------------------------------------------
            HttpContext.SignInAsync(userPrincipal);

            return(RedirectToAction("Index", "Home"));
        }
Exemplo n.º 2
0
        public IActionResult Login(UserPassModel userPass)
        {
            //string userString = HttpContext.Session.GetString("user");
            if (userPass.username == null)
            {
                ModelState.Remove("username");
                ModelState.Remove("password");
                //auto sign in
                return(View("Login"));
            }
            else
            {
                if (ServerSideValidation.ValidateInfo(userPass))
                {
                    if (UserDatabaseContext.Login(userPass))
                    {
                        //Use Db context to validate user name and pass
                        TempData["User"] = userPass.username;

                        User user = new User();
                        user.username = userPass.username;

                        user.accessToken = UserDatabaseContext.GenerateAccessToken(userPass.username);

                        HttpContext.Session.SetString("user", JsonConvert.SerializeObject(user));

                        return(RedirectToAction("Index"));
                    }
                    else
                    {
                        TempData["err"] = "Invalid username or password";
                        return(View());
                    }
                }
                return(View());
            }
        }