コード例 #1
0
        public async Task <IActionResult> Registration(RegisterModel model)
        {
            Logger logger  = LogManager.GetCurrentClassLogger();
            var    results = new List <ValidationResult>();

            if (!Validator.TryValidateObject(model, new ValidationContext(model), results, true))
            {
                foreach (var err in results)
                {
                    logger.Info(getLogInfo(HttpContext) + model.userName + " " + err);
                }
            }

            else
            {
                var currClient = db.clientsList.Where(p => p.name == model.userName).FirstOrDefault();

                if (currClient != null)
                {
                    ViewBag.Error = "Пользователь с этим именем уже существует.";
                    logger.Info(getLogInfo(HttpContext) + model.userName + ": Пользователь с этим именем уже существует.");
                }
                else
                {
                    db.clientsList.Add(new DBClient()
                    {
                        name = model.userName, pas = model.password, mail = model.email
                    });
                    db.SaveChanges();
                    var userIdentity = new ClaimsIdentity(new List <Claim>
                    {
                        new Claim(ClaimTypes.Name, model.userName)
                    }, CookieAuthenticationDefaults.AuthenticationScheme, ClaimsIdentity.DefaultNameClaimType, ClaimsIdentity.DefaultRoleClaimType);

                    await HttpContext.SignInAsync(new ClaimsPrincipal(userIdentity), new AuthenticationProperties()
                    {
                        IsPersistent = false
                    });

                    logger.Info(getLogInfo(HttpContext) + model.userName + ": Вход успешно выполнен");
                    return(RedirectToAction("Index", "Home"));
                }
            }

            return(View());
        }