As you say in the EDIT part, you must first add the administrator account manually. After that, you can simply provide the Admin role to any registered user, like this:
Roles.AddUserToRole(model.UserName, "Admin");
If you want your administrators to be completely separate from other users, you must place the user role for regular users and not allow administrators to have this role.
So, just add the above code to the Register method of the AccountController.cs action
[HttpPost] [Authorize(Roles="A, Personnels")] [ValidateAntiForgeryToken] public ActionResult Register(RegisterModel model) { if (ModelState.IsValid) { // Attempt to register the user try { WebSecurity.CreateUserAndAccount(model.UserName, model.Password); Roles.AddUserToRole(model.UserName, "User"); // Add this line here... WebSecurity.Login(model.UserName, model.Password); return RedirectToAction("Index", "Home"); } catch (MembershipCreateUserException e) { ModelState.AddModelError("", ErrorCodeToString(e.StatusCode)); } } // If we got this far, something failed, redisplay form return View(model); }
And take responsibility for creating administrator accounts yourself, or let them register as regular users and do the following [The first approach is recommended]:
Roles.RemoveUserFromRole(model.UserName, "User"); Roles.AddUserToRole(model.UserName, "Admin");
That's all...
source share