Exemplo n.º 1
0
 public IActionResult Login(string userName, string password, string returnUrl)
 {
     try
     {
         _logger.LogInformation($"登录:UserName={userName}");
         var userRole = _userReposity.GetUser(userName, password);
         if (userRole != null)
         {
             var claims = new Claim[]
             {
                 new Claim(ClaimTypes.Role, userRole.RoleId.ToString() == "1"?"Manager":"User"),
                 new Claim(ClaimTypes.Name, "刘"),
                 new Claim(ClaimTypes.Sid, userRole.Id.ToString()),
                 new Claim(ClaimTypes.GroupSid, userRole.Id.ToString()),
             };
             HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(new ClaimsIdentity(claims)));
             return(new RedirectResult(string.IsNullOrEmpty(returnUrl) ? "/" : returnUrl));
         }
         else
         {
             ViewBag.error = "用户名或者密码错误";
             return(new ViewResult());
         }
     }
     catch (Exception exc)
     {
         ViewBag.error = exc.Message;
         _logger.LogCritical(exc, $"登录异常:{ exc.Message}");
         return(new ViewResult());
     }
 }
        public void LoginTest()
        {
            var user = _userReposity.GetUser("peter", "peter");

            Assert.NotNull(user);
        }