public async Task <ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { var user = new ApplicationUser { UserName = model.UserName, Email = model.Email }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 // Send an email with this link // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); //Assign Role to user Here await this.UserManager.AddToRoleAsync(user.Id, model.SelectedUserRole); // end here return(RedirectToAction("Index", "Home")); } var myHelper = new ControllersHelper(); model.UserRoles = myHelper.GetRolesForUser(context, UserManager); AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }
public ActionResult Register() { var myHelper = new ControllersHelper(); RegisterViewModel model = new RegisterViewModel { UserRoles = myHelper.GetRolesForUser(context, UserManager), SelectedUserRole = RoleConstants.Client }; return(View(model)); }
public async Task <ActionResult> ChangeInformation(ChangeInformationViewModel model) { var myHelper = new ControllersHelper(); if (!ModelState.IsValid) { model.UserRoles = myHelper.GetRolesForUser(context, UserManager, User); model.SelectedUserRole = UserManager.GetRoles(User.Identity.GetUserId()).FirstOrDefault(); return(View(model)); } IdentityResult resultUser = null; var user = await UserManager.FindByIdAsync(User.Identity.GetUserId()); if (user != null) { user.UserName = model.UserName; user.Email = model.Email; resultUser = UserManager.Update(user); if (resultUser.Succeeded) { // roles delete var roles = await UserManager.GetRolesAsync(User.Identity.GetUserId()); IdentityResult resultRole = await UserManager.RemoveFromRolesAsync(User.Identity.GetUserId(), roles.ToArray()); if (resultRole.Succeeded) { // add role await this.UserManager.AddToRoleAsync(user.Id, model.SelectedUserRole); await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); return(RedirectToAction("Index", new { Message = ManageMessageId.ChangeInformationSuccess })); } } } model.UserRoles = myHelper.GetRolesForUser(context, UserManager, User); model.SelectedUserRole = UserManager.GetRoles(User.Identity.GetUserId()).FirstOrDefault(); AddErrors(resultUser); return(View(model)); }
// // GET: /Manage/ChangeInformation public ActionResult ChangeInformation() { var myHelper = new ControllersHelper(); var userId = User.Identity.GetUserId(); var user = context.Users.Where(u => u.Id == userId).FirstOrDefault(); ChangeInformationViewModel model = new ChangeInformationViewModel { SelectedUserRole = UserManager.GetRoles(User.Identity.GetUserId()).FirstOrDefault(), UserName = user.UserName, Email = user.Email, UserRoles = myHelper.GetRolesForUser(context, UserManager, User) }; return(View(model)); }