public ActionResult AddUserToRoleById(string id) { AddUserToRoleById model = new AddUserToRoleById { Roles = new List <string>(), UserId = id }; model.Roles.Add("Admin"); model.Roles.Add("Editor"); model.Roles.Add("Moderator"); model.Roles.Add("User"); model.Roles.Add("Banned"); return(View(model)); }
public ActionResult AddUserToRoleById(AddUserToRoleById model) { var user = UserManager.FindById(model.UserId); var roles = UserManager.GetRoles(user.Id); Debug.WriteLine(String.Join(Environment.NewLine, roles)); UserManager.RemoveFromRoles(user.Id, roles.ToArray()); UserManager.AddToRole(user.Id, model.Role); return(RedirectToAction("Index", "Posts")); }
public async Task <ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { var user = new ApplicationUser { UserName = model.Email, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName, BirthDay = model.BirthDay, Biography = model.Biography, ProfilePictureURL = model.ProfilePictureURL }; 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 https://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>"); Debug.WriteLine("Uesr.id: " + user.Id); Debug.WriteLine("Role Id" + db.Roles.Single(r => r.Name.Equals("User")).Id + " " + db.Roles.Single(r => r.Name.Equals("User")).Name); AddUserToRoleById addUserToRoleById = new AddUserToRoleById() { UserId = user.Id, Role = "User" }; AddUserToRoleById(addUserToRoleById); //return RedirectToAction("Index", "Home"); return(RedirectToAction("HomeView", "Categories")); } AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }