Пример #1
0
        public async Task <IActionResult> Register(LoginViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new User()
                {
                    UserName = model.UserName,
                    Email    = model.Email
                };

                var result = await _userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    await _userManager.AddToRoleAsync(user, model.Role);

                    await _signInManager.SignOutAsync();

                    var loginResult = await _signInManager.PasswordSignInAsync(user, model.Password, false, false);

                    if (loginResult.Succeeded && model.Role == "Employer")
                    {
                        var employer = new Employer()
                        {
                            User   = user,
                            UserId = user.Id
                        };
                        _employerService.CreateItem(employer);
                        return(RedirectToAction("Index", "Home"));
                    }
                    else if (loginResult.Succeeded && model.Role == "Employee")
                    {
                        var employee = new Employee()
                        {
                            User   = user,
                            UserId = user.Id
                        };
                        _employeeService.CreateItem(employee);
                        return(RedirectToAction("FillDetails", "Employee"));
                    }
                    else if (loginResult.Succeeded && model.Role == "Admin")
                    {
                        return(RedirectToAction("Index", "Admin"));
                    }
                    else
                    {
                        model.Error = loginResult.ToString();
                        return(View(model));
                    }
                }
                else
                {
                    model.Error = result.Errors.ElementAt(0).Description;
                    return(View(model));
                }
            }
            return(View(model));
        }