public int Login(string AccountName, string Pwd, string yz)
        {
            if (Session["SecurityCode"].ToString().ToUpper() != yz.ToUpper())
            {
                return(-2);
            }

            List <Users> list = user.Login(AccountName, Pwd);

            if (list.Count > 0)
            {
                Session["username"] = AccountName;
                Session["User"]     = list.FirstOrDefault();
                //在登录成功处判断
                if (Convert.ToInt32(list.FirstOrDefault().State) == 0)
                {
                    int id = list.FirstOrDefault().ID;
                    int a  = user.StateUpt(id);
                    logs.Add(list[0].ID, "登录", 1);
                    return(a);
                }
                else
                {
                    logs.Add(list[0].ID, "登录", 0);
                    return(-5);
                }
            }
            return(0);
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Login(string userName, string password, string returnUrl = null)
        {
            var user = _userRepository.Login(userName, password);

            if (user != null)
            {
                //用户标识
                var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);

                identity.AddClaim(new Claim(ClaimTypes.Name, user.Name));
                identity.AddClaim(new Claim(ClaimTypes.Role, user.RoleName));
                identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.UserName));
                identity.AddClaim(new Claim(ClaimTypes.Sid, user.UserID.ToString()));
                await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(identity));

                if (returnUrl == null)
                {
                    returnUrl = TempData["returnUrl"]?.ToString();
                }
                if (returnUrl != null)
                {
                    return(Redirect(returnUrl));
                }
                else
                {
                    return(RedirectToAction(nameof(HomeController.Index), "Home"));
                }
            }
            else
            {
                const string badUserNameOrPasswordMessage = "用户名或密码错误!";
                return(BadRequest(badUserNameOrPasswordMessage));
            }
        }
Exemplo n.º 3
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());
            }
        }