Exemplo n.º 1
0
        public async Task <IActionResult> Login(string loginName, string password)
        {
            //需要加入登录次数限制
            if (string.IsNullOrEmpty(loginName) && string.IsNullOrEmpty(password))
            {
                return(Json(new { code = 1, msg = "失败" }));
            }
            else
            {
                var user = await userInfoRepository.GetAsync(loginName, MD5Hash(password));

                if (user != null)
                {
                    //用户标识
                    var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
                    identity.AddClaim(new Claim(ClaimTypes.Sid, loginName));
                    identity.AddClaim(new Claim(ClaimTypes.Name, user.UserName));
                    identity.AddClaim(new Claim(ClaimTypes.Role, user.Permission.PermissionName));
                    await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(identity));

                    //@User.Claims.SingleOrDefault(s=>s.Type==System.Security.Claims.ClaimTypes.Sid).Value 获取用户名
                    return(Redirect("index"));
                }
                return(Redirect("Login"));//Json(new { code = 1, msg = "失败" });
            }
        }