Ejemplo n.º 1
0
        public async Task <ActionResult> Register(UserInfoAccoutViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                RegisterViewModel userViewModel = new RegisterViewModel
                {
                    Username        = viewModel.Username,
                    ConfirmPassword = viewModel.ConfirmPassword,
                    Email           = viewModel.Email,
                    Password        = viewModel.Password
                };
                UserInfo userInfo = new UserInfo
                {
                    Company         = viewModel.Company,
                    CountExperience = viewModel.CountExperience,
                    CurrentJob      = viewModel.CurrentJob,
                    FullName        = viewModel.FullName,
                    TimeUnit        = viewModel.TimeUnit,
                    UserName        = viewModel.Username,
                    IsActive        = true,
                    Email           = viewModel.Email
                };

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

                if (result.Succeeded)
                {
                    //insert role
                    var roleManager = HttpContext.GetOwinContext().GetUserManager <ApplicationRoleManager>();
                    //var role = roleManager.Roles.Where(r => r.Name.Equals("Normal User")).FirstOrDefault();
                    var x = await UserManager.AddToRoleAsync(user.Id, "Normal User");

                    if (x.Succeeded)
                    {
                        //insert user info
                        var resultUserInfo = await _userBus.Create(userInfo, new List <string>());

                        if (!resultUserInfo)
                        {
                            var userDelete = await UserManager.FindByEmailAsync(viewModel.Email);

                            if (userDelete != null)
                            {
                                await UserManager.DeleteAsync(userDelete);
                            }

                            return(Json(new { Status = true, Message = "Resgister failed!" }));
                        }
                        else
                        {
                            var code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);

                            var callbackUrl = Url.Action(
                                "ConfirmEmail", "Account",
                                new { userId = user.Id, code = code },
                                protocol: Request.Url.Scheme);

                            var emailContent = "Please confirm your account by clicking this link: <a href=\"" + callbackUrl + "\">link</a>";
                            // ViewBag.Link = callbackUrl;   // Used only for initial demo.

                            SendMail(new List <string>(), viewModel.Email, "[PROMANA] Please confirm your email!", emailContent);
                            return(View("DisplayEmail"));
                        }
                    }
                }
                AddErrors(result);
            }
            // If we got this far, something failed, redisplay form
            return(View());
        }
Ejemplo n.º 2
0
        public async Task <ActionResult> Create(UserInfoAccoutViewModel viewModel, params string[] selectedRoles)
        {
            if (ModelState.IsValid)
            {
                RegisterViewModel userViewModel = new RegisterViewModel {
                    Username        = viewModel.Username,
                    ConfirmPassword = viewModel.ConfirmPassword,
                    Email           = viewModel.Email,
                    Password        = viewModel.Password
                };
                UserInfo userInfo = new UserInfo
                {
                    Company         = viewModel.Company,
                    CountExperience = viewModel.CountExperience,
                    CurrentJob      = viewModel.CurrentJob,
                    FullName        = viewModel.FullName,
                    TimeUnit        = viewModel.TimeUnit,
                    UserName        = viewModel.Username,
                    IsActive        = true,
                    Email           = viewModel.Email
                };

                var user = new ApplicationUser {
                    UserName = userViewModel.Username, Email = userViewModel.Email, EmailConfirmed = true
                };
                var adminresult = await UserManager.CreateAsync(user, userViewModel.Password);

                //Add User to the selected Roles
                if (adminresult.Succeeded)
                {
                    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
                        {
                            //insert user info
                            var resultUserInfo = await _userBus.Create(userInfo, new List <string>());

                            if (!resultUserInfo)
                            {
                                var userDelete = await UserManager.FindByEmailAsync(viewModel.Email);

                                if (userDelete != null)
                                {
                                    await UserManager.DeleteAsync(userDelete);
                                }
                                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());
                }
                TempData["isSuccess"] = true;
                return(RedirectToAction("Index"));
            }
            ViewBag.RoleId = new SelectList(RoleManager.Roles, "Name", "Name");
            return(View());
        }