Exemplo n.º 1
0
        public IActionResult Login(string userName, string password, string returnUrl)
        {
            //查询users
            dynamic user = _userRepository.Login(userName, password);

            if (user != null)
            {
                //查询角色名称
                dynamic roleName = _userRepository.GetRole(user.RoleID).RoleName;

                var claims = new Claim[]
                {
                    new Claim(ClaimTypes.UserData, user.UserName),
                    new Claim(ClaimTypes.Role, roleName),
                    new Claim(ClaimTypes.Name, user.Name),
                    new Claim(ClaimTypes.PrimarySid, user.ID.ToString())
                };
                HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
                HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(new ClaimsIdentity(claims)));
                return(new RedirectResult(returnUrl == null ? "/home/index" : returnUrl));
            }
            else
            {
                ViewBag.error = "用户名或密码错误!";
                return(View());
            }
        }