コード例 #1
0
        public IActionResult Login(Users user)
        {
            if (!string.IsNullOrEmpty(user.userName) && string.IsNullOrEmpty(user.userPassword))
            {
                return(RedirectToAction("Login"));
            }

            ClaimsIdentity identity        = null;
            bool           isAuthenticated = false;
            string         Direct          = "Index";
            Boolean        temp            = usersRepo.CheckLogin(user.userName, user.userPassword);

            if (temp == true)
            {
                if ((user.userName == "admin"))
                {
                    identity = new ClaimsIdentity(new[] {
                        new Claim(ClaimTypes.Name, user.userName),
                        new Claim(ClaimTypes.Role, "Admin")
                    }, CookieAuthenticationDefaults.AuthenticationScheme);
                    isAuthenticated = true;
                    Direct          = "Setting";
                }
                else
                {
                    identity = new ClaimsIdentity(new[] {
                        new Claim(ClaimTypes.Name, user.userName),
                        new Claim(ClaimTypes.Role, "User")
                    }, CookieAuthenticationDefaults.AuthenticationScheme);

                    isAuthenticated = true;
                    Direct          = "Index";
                }
                if (isAuthenticated)
                {
                    var principal = new ClaimsPrincipal(identity);

                    var login = HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal);

                    return(RedirectToAction(Direct, "Home"));
                }
                return(View());
            }
            else
            {
                ViewBag.Message = "Username or Password are not correct, please fill againts";
                return(View());
            }
        }