public ActionResult Index()
        {
            var user = _repository.GetById <UserModel, string>(User.FindFirstValue(ClaimTypes.NameIdentifier));

            if (user == null || !user.State.Equals(BuildingAdmin.DataLayer.Models.UserStatesEnum.Active))
            {
                return(StatusCode(404));
            }
            return(View(new SettingsModel(user)));
        }
Пример #2
0
        public async Task <IActionResult> Login(UserViewModel model)
        {
            SetCaptchaErrors();
            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            var user = _repository.GetById <UserModel, string>(model.Email);

            if (user == null && user.State.Equals(UserStatesEnum.Active))
            {
                ModelState.AddModelError("", _localizer["Invalid email or wrong password"]);
                return(View(model));
            }

            if (user != null && _argonHash.Encoder(model.Password, _hashingConfig.Value, user.Salt).Hash.Equals(user.Password))
            {
                var identity = new ClaimsIdentity(new [] {
                    new Claim(ClaimTypes.Surname, $"{user.FirstName} {user.LastName}"),
                    new Claim(ClaimTypes.NameIdentifier, user.Id)
                }, CookieAuthenticationDefaults.AuthenticationScheme);

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

                var principal = new ClaimsPrincipal(identity);

                await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal, new AuthenticationProperties {
                    IsPersistent = model.RememberMe
                });

                return(RedirectToAction("Index", "Landing"));
            }
            ModelState.AddModelError("", _localizer["Invalid email or wrong password"]);
            return(View(model));
        }