Exemplo n.º 1
0
        private async Task Authenticate(ClientWebView model)
        {
            // создаем один claim

            var claims = new List <Claim>
            {
                new Claim(ClaimsIdentity.DefaultNameClaimType, model.Email),
                new Claim(ClaimsIdentity.DefaultRoleClaimType, ((RoleType)model.idRole).ToString())
            };
            // создаем объект ClaimsIdentity
            ClaimsIdentity id = new ClaimsIdentity(claims, "ApplicationCookie", ClaimsIdentity.DefaultNameClaimType, ClaimsIdentity.DefaultRoleClaimType);
            // установка аутентификационных куки
            await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(id));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Register(ClientWebView model)
        {
            if (ModelState.IsValid)
            {
                model.idRole = 1;
                if (_acount.Register(_mapper.Map <ClientBl>(model)))
                {
                    // добавляем пользователя в бд


                    await Authenticate(model); // аутентификация

                    return(RedirectToAction("Index", "Home"));
                }
                else
                {
                    ModelState.AddModelError("", "Некорректные логин и(или) пароль");
                }
            }
            return(View(model));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> Login(Models.LoginViewModel model)
        {
            if (ModelState.IsValid)
            {
                var client = _acount.Login(_mapper.Map <BusinessLogic.Models.LoginModel>(model));
                if (client != null)
                {
                    var clientWeb = new ClientWebView
                    {
                        Email    = client.Email,
                        Password = client.Password,
                        idRole   = client.idRole
                    };
                    await Authenticate(clientWeb); // аутентификация

                    return(RedirectToAction("Index", "Home"));
                }
                ModelState.AddModelError("", "Некорректные логин и(или) пароль");
            }
            return(View(model));
        }