public ActionResult Register(RegisterModel model) { if(WebSecurity.UserExists(model.UserName)) ModelState.AddModelError("Name", "This User name is already registered"); if(model.SelectedRole.Equals(RoleList.Country) && String.IsNullOrEmpty(model.SelectedCountry)) ModelState.AddModelError("SelectedCountry", "Select a country for this user"); if (ModelState.IsValid) { try { String password = Membership.GeneratePassword(10,0); WebSecurity.CreateUserAndAccount(model.UserName, password, new { Country = model.SelectedCountry, Email = model.Email }); Roles.AddUserToRole(model.UserName, model.SelectedRole); IDictionary<string, string> data = new Dictionary<string, string>(); data.Add("name",model.UserName); data.Add("password", password); String subject = String.Format("\"{0}\" - You can now connect to SPC VMS-Tufman Reconciliation ", model.UserName); String body = new TemplatingService().GetTemplatedDocument("NewUserEmail.vm", data); new EmailServices().SendEmail(subject, body, model.Email); MessageModel messageModel = new MessageModel(); messageModel.Body = "An email has been sent to this user with its credentials"; return View("Message", messageModel); } catch (MembershipCreateUserException e) { ModelState.AddModelError("", ErrorCodeToString(e.StatusCode)); } } // If we got this far, something failed, redisplay form model.Roles = Roles.GetAllRoles().Select(c => new SelectListItem { Value = c, Text = c }).ToList(); model.Countries = _repo.GetAll<TufmanCountry>().Select(c => new SelectListItem { Value = c.Code, Text = c.Label }).ToList(); return View(model); }
public ActionResult Register() { RegisterModel registerModel = new RegisterModel(); registerModel.Roles = Roles.GetAllRoles().Select(c => new SelectListItem { Value = c, Text = c }).ToList(); registerModel.Countries = _repo.GetAll<TufmanCountry>().Select(c => new SelectListItem { Value = c.Code, Text = c.Label }).ToList(); return View(registerModel); }