示例#1
0
        public async Task <OperationResult> SignInAsync(string userName, string password, bool isPersistent)
        {
            var user = citizenUserRepository.GetUserByNameAndPassword(userName, password);

            if (user == null)
            {
                return(OperationResult.Failed("Login or password is incorrect."));
            }

            var claimsIdentity = new ClaimsIdentity("CookieAuth");

            claimsIdentity.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()));
            claimsIdentity.AddClaim(new Claim(ClaimTypes.Name, user.Login));
            claimsIdentity.AddClaim(new Claim(ClaimTypes.AuthenticationMethod, "CookieAuth"));

            foreach (var role in user.Roles)
            {
                claimsIdentity.AddClaim(new Claim(ClaimTypes.Role, role.Name));
            }

            var userPrincipal = new ClaimsPrincipal(claimsIdentity);

            await httpContextAccessor.HttpContext.Authentication.SignInAsync("CookieAuth", userPrincipal, new AuthenticationProperties { IsPersistent = isPersistent });

            return(OperationResult.Success());
        }
        public async Task <IActionResult> Login(ForDHLoginViewModel loginView)
        {
            var user = citizenRepository.GetUserByNameAndPassword(loginView.Login, loginView.Password);

            if (user == null)
            {
                return(View(loginView));
            }

            //var recordId = new Claim("Id", user.Id.ToString());
            //var recordName = new Claim(ClaimTypes.Name, user.Login);
            //var recordAuthMetod = new Claim(ClaimTypes.AuthenticationMethod, Startup.MedicineAuth);

            //var page = new List<Claim>() { recordId, recordName, recordAuthMetod };

            //var claimsIdentity = new ClaimsIdentity(page, Startup.MedicineAuth);

            //var claimsPrincipal = new ClaimsPrincipal(claimsIdentity);

            //await HttpContext.SignInAsync(claimsPrincipal);

            await userService.SignInAsync(loginView.Login, loginView.Password, isPersistent : false);

            if (string.IsNullOrEmpty(loginView.ReturnUrl))
            {
                return(RedirectToAction("HealthDepartment", "HealthDepartment"));
            }
            else
            {
                return(Redirect(loginView.ReturnUrl));
            }
        }