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(Startup.AuthMethod);

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

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

            var userPrincipal = new ClaimsPrincipal(claimsIdentity);

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

            return(OperationResult.Success());
        }
Exemple #2
0
        public async Task <IActionResult> Login(LifeLoginViewModel loginViewModel)
        {
            if (!ModelState.IsValid)
            {
                return(View(loginViewModel));
            }

            var user = citizenUserRepository
                       .GetUserByNameAndPassword(loginViewModel.Login, loginViewModel.Password);

            if (user == null)
            {
                ModelState.AddModelError(String.Empty, "Такого логина и пароля не существует.");
                return(View(loginViewModel));
            }

            var recordId         = new Claim(ClaimTypes.NameIdentifier, user.Id.ToString());
            var recordName       = new Claim(ClaimTypes.Name, user.Login);
            var recordAuthMethod = new Claim(ClaimTypes.AuthenticationMethod, Startup.LifeAuth);
            var claims           = new List <Claim>()
            {
                recordId, recordName, recordAuthMethod
            };
            var claimsIdentity  = new ClaimsIdentity(claims, Startup.LifeAuth);
            var claimsPrincipal = new ClaimsPrincipal(claimsIdentity);

            await HttpContext.SignInAsync(claimsPrincipal);

            if (string.IsNullOrEmpty(loginViewModel.ReturnUrl))
            {
                return(RedirectToAction("Index", "Life"));
            }

            return(Redirect(loginViewModel.ReturnUrl));
        }
        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));
            }
        }
Exemple #4
0
        public async Task <IActionResult> Login(LoginViewModel loginViewModel)
        {
            var user = citizenUserRepository
                       .GetUserByNameAndPassword(loginViewModel.Login, loginViewModel.Password);

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

            //Строки в документе
            var recordId    = new Claim("Id", user.Id.ToString());
            var recordName  = new Claim(ClaimTypes.Name, user.Login);
            var placeOfWork = "user";

            if (user.PlaceOfWork != null)
            {
                placeOfWork = user.PlaceOfWork;
            }
            var recordPosition = new Claim("PlaceOfWork", placeOfWork);

            var recordAuthMethod = new Claim(ClaimTypes.AuthenticationMethod, Startup.AuthMethod);

            //Страница в документе
            var page = new List <Claim>()
            {
                recordId, recordName, recordPosition, recordAuthMethod
            };

            //Документ
            var claimsIdentity = new ClaimsIdentity(page, Startup.AuthMethod);

            //Пользователь с точки зрения .net
            var claimsPrincipal = new ClaimsPrincipal(claimsIdentity);

            await HttpContext.SignInAsync(claimsPrincipal);

            if (string.IsNullOrEmpty(loginViewModel.ReturnUrl))
            {
                return(RedirectToAction("Index", "Home"));
            }
            else
            {
                return(Redirect(loginViewModel.ReturnUrl));
            }
        }
        public async Task <IActionResult> Login(ForDHLoginViewModel loginView)
        {
            var user = citizenRepository.GetUserByNameAndPassword(loginView.Login, loginView.Password);

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


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

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