Esempio n. 1
0
        public async Task <ActionResult> Create(RegisterViewModel userViewModel, params string[] selectedRoles)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = userViewModel.Email, Email = userViewModel.Email
                };
                var adminresult = await UserManager.CreateAsync(user, userViewModel.Password);

                //Add User to the selected Roles
                if (adminresult.Succeeded)
                {
                    #region Dealing with custom user details
                    UserDet newUserDeets = new UserDet();
                    newUserDeets.UserID              = user.Id;
                    newUserDeets.FirstName           = userViewModel.FirstName;
                    newUserDeets.LastName            = userViewModel.LastName;
                    newUserDeets.DateStarted         = userViewModel.DateStarted;
                    newUserDeets.IsEmployedCurrently = userViewModel.IsEmployedCurrently;


                    CoffeeShopLMSEntities db = new CoffeeShopLMSEntities();
                    db.UserDets.Add(newUserDeets);
                    db.SaveChanges();
                    #endregion

                    if (selectedRoles != null)
                    {
                        var result = await UserManager.AddToRolesAsync(user.Id, selectedRoles);

                        if (!result.Succeeded)
                        {
                            ModelState.AddModelError("", result.Errors.First());
                            ViewBag.RoleId = new SelectList(await RoleManager.Roles.ToListAsync(), "Name", "Name");
                            return(View());
                        }
                    }
                }
                else
                {
                    ModelState.AddModelError("", adminresult.Errors.First());
                    ViewBag.RoleId = new SelectList(RoleManager.Roles, "Name", "Name");
                    return(View());
                }
                return(RedirectToAction("Index"));
            }
            ViewBag.RoleId = new SelectList(RoleManager.Roles, "Name", "Name");
            return(View());
        }