Пример #1
0
        public async Task <ActionResult> UserRegistration(UserViewModel userViewModel, string ButtonType)
        {
            if (ButtonType == "Next")
            {
                if (ModelState.IsValid)
                {
                    CurrentUserModel = userViewModel;
                    ApplicationDbContext context = new ApplicationDbContext();

                    var UserManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(context));

                    var user = new ApplicationUser {
                        UserName = CurrentUserModel.Username, Email = CurrentUserModel.Email, DisplayName = CurrentStaff.GetFullName()
                    };
                    var result = await UserManager.CreateAsync(user, CurrentUserModel.Password);

                    if (result.Succeeded)
                    {
                        if (CurrentStaff.StaffType == BLL.Constants.StaffTypes.TEACHER)
                        {
                            UserManager.AddToRole(user.Id, "Teacher");
                            var teacher = new Teacher()
                            {
                                ID = CurrentStaff.ID
                            };
                            _unitOfWork.Teachers.Add(teacher);
                        }


                        CurrentStaff.user_id = user.Id;

                        _service.Update(CurrentStaff);
                        _unitOfWork.Addresss.Update(CurrentStaff.Address); // just to make sure



                        try
                        {
                            ApplicationSignInManager SignInManager = HttpContext.GetOwinContext().Get <ApplicationSignInManager>();
                            await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                            await _unitOfWork.SaveAsync();
                        }
                        catch (DbUpdateConcurrencyException)
                        {
                        }


                        ModelState.Clear();

                        return(View("SuccessRegistration", CurrentStaff));
                    }
                }
            }


            else if (ButtonType == "Back")
            {
                return(View("StaffInfo", CurrentStaff));
            }



            return(View());
        }