Пример #1
0
        public async Task <IActionResult> Login(LoginViewModel vm)
        {
            if (ModelState.IsValid && _accountServices.CheckCredentials(vm.Username, vm.Password))
            {
                var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
                identity.AddClaim(new Claim(ClaimTypes.Name, vm.Username));
                foreach (var roleName in _accountServices.GetRolesForUser(vm.Username))
                {
                    identity.AddClaim(new Claim(ClaimTypes.Role, roleName));
                }
                var principal = new ClaimsPrincipal(identity);
                await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal);

                return(RedirectToAction("Index", "Home"));
            }
            vm.ErrorMessage = "Not authorized";
            return(View(vm));
        }