title: Only allow to register users to the admin in ASP.NET MVC
body: <p>In my scenario, the users can not register, there will be an admin, and that admin will create the users with a default password they can change later.</p>
  
  <p>I am reviewing the pluralsight videos and according to those I should put the [Authorize] tag in the action method.</p>
  
  <p>I did the following, but once I did this, I cant see anything on <a href="http://localhost/Account/Register" rel="nofollow noreferrer">http://localhost/Account/Register</a>, neither as a logged in user.</p>
  
  <ol>
  <li>The role admin already exists.</li>
  <li>The logged in user is also associated to that role in the UserInRoles table.</li>
  </ol>
  
  <p>If I remove the [Authorize] attribute, then I can access the register page.</p>
  
  <pre><code>// GET: /Account/Register
      [HttpPost]
      [Authorize(Roles = "admin")]
      public ActionResult Register()
      {
          return View();
      }
  
      //
      // POST: /Account/Register
  
      [HttpPost]
      [Authorize(Roles="admin")]
      public ActionResult Register(RegisterModel model)
      {
          if (ModelState.IsValid)
          {
              // Attempt to register the user
              MembershipCreateStatus createStatus;
              Membership.CreateUser(model.UserName, model.Password, model.Email, null, null, true, null, out createStatus);
  
              if (createStatus == MembershipCreateStatus.Success)
              {
                  FormsAuthentication.SetAuthCookie(model.UserName, false /* createPersistentCookie */);
                  return RedirectToAction("Index", "Home");
              }
              else
              {
                  ModelState.AddModelError("", ErrorCodeToString(createStatus));
              }
          }
  
          // If we got this far, something failed, redisplay form
          return View(model);
      }
  </code></pre>
  
  <p>The logon partial is:</p>
  
  <pre><code>@if(Request.IsAuthenticated) {
      &lt;text&gt;Welcome &lt;strong&gt;@User.Identity.Name&lt;/strong&gt;!
      [ @Html.ActionLink("Register", "Register", "Account") ]
      [ @Html.ActionLink("Log Off", "LogOff", "Account") ]
      [ @Html.ActionLink("Change Password", "ChangePassword", "Account") ]
      [ @Html.ActionLink("Position", "Position", "Position") ]
      [ @Html.ActionLink("User Position", "Position", "UserPositionPosition") ]
      &lt;/text&gt;
  }
  else {
      @:[ @Html.ActionLink("Log On", "LogOn", "Account") ]
  }
  
  
  
  
  
  
  
  
   EDIT: I better attached the full account controller for better understanding
  
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.Mvc;
  using System.Web.Routing;
  using System.Web.Security;
  using HRRazorForms.Models;
  
  namespace HRRazorForms.Controllers
  {
      public class AccountController : Controller
      {
  
          //
          // GET: /Account/LogOn
  
          public ActionResult LogOn()
          {
              return View();
          }
  
          //
          // POST: /Account/LogOn
  
          [HttpPost]
          public ActionResult LogOn(LogOnModel model, string returnUrl)
          {
              if (ModelState.IsValid)
              {
                  if (Membership.ValidateUser(model.UserName, model.Password))
                  {
                      FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                      if (Url.IsLocalUrl(returnUrl) &amp;&amp; returnUrl.Length &gt; 1 &amp;&amp; returnUrl.StartsWith("/")
                          &amp;&amp; !returnUrl.StartsWith("//") &amp;&amp; !returnUrl.StartsWith("/\\"))
                      {
                          return Redirect(returnUrl);
                      }
                      else
                      {
                          return RedirectToAction("Index", "Home");
                      }
                  }
                  else
                  {
                      ModelState.AddModelError("", "The user name or password provided is incorrect.");
                  }
              }
  
              // If we got this far, something failed, redisplay form
              return View(model);
          }
  
          //
          // GET: /Account/LogOff
  
          public ActionResult LogOff()
          {
              FormsAuthentication.SignOut();
  
              return RedirectToAction("Index", "Home");
          }
  
          //
          // GET: /Account/Register
          [HttpPost]
          [Authorize(Roles = "admin")]       
          public ActionResult Register()
          {
              return View();
          }
  
          //
          // POST: /Account/Register
  
          [HttpPost]
          [Authorize(Roles="admin")]
          public ActionResult Register(RegisterModel model)
          {
              if (ModelState.IsValid)
              {
                  // Attempt to register the user
                  MembershipCreateStatus createStatus;
                  Membership.CreateUser(model.UserName, model.Password, model.Email, null, null, true, null, out createStatus);
  
                  if (createStatus == MembershipCreateStatus.Success)
                  {
                      //FormsAuthentication.SetAuthCookie(model.UserName, false /* createPersistentCookie */);
                      return RedirectToAction("Index", "Home");
                  }
                  else
                  {
                      ModelState.AddModelError("", ErrorCodeToString(createStatus));
                  }
              }
  
              // If we got this far, something failed, redisplay form
              return View(model);
          }
  
          //
          // GET: /Account/ChangePassword
  
          [Authorize]
          public ActionResult ChangePassword()
          {
              return View();
          }
  
          //
          // POST: /Account/ChangePassword
  
          [Authorize]
          [HttpPost]
          public ActionResult ChangePassword(ChangePasswordModel model)
          {
              if (ModelState.IsValid)
              {
  
                  // ChangePassword will throw an exception rather
                  // than return false in certain failure scenarios.
                  bool changePasswordSucceeded;
                  try
                  {
                      MembershipUser currentUser = Membership.GetUser(User.Identity.Name, true /* userIsOnline */);
                      changePasswordSucceeded = currentUser.ChangePassword(model.OldPassword, model.NewPassword);
                  }
                  catch (Exception)
                  {
                      changePasswordSucceeded = false;
                  }
  
                  if (changePasswordSucceeded)
                  {
                      return RedirectToAction("ChangePasswordSuccess");
                  }
                  else
                  {
                      ModelState.AddModelError("", "The current password is incorrect or the new password is invalid.");
                  }
              }
  
              // If we got this far, something failed, redisplay form
              return View(model);
          }
  
          //
          // GET: /Account/ChangePasswordSuccess
  
          public ActionResult ChangePasswordSuccess()
          {
              return View();
          }
  
          #region Status Codes
          private static string ErrorCodeToString(MembershipCreateStatus createStatus)
          {
              // See http://go.microsoft.com/fwlink/?LinkID=177550 for
              // a full list of status codes.
              switch (createStatus)
              {
                  case MembershipCreateStatus.DuplicateUserName:
                      return "User name already exists. Please enter a different user name.";
  
                  case MembershipCreateStatus.DuplicateEmail:
                      return "A user name for that e-mail address already exists. Please enter a different e-mail address.";
  
                  case MembershipCreateStatus.InvalidPassword:
                      return "The password provided is invalid. Please enter a valid password value.";
  
                  case MembershipCreateStatus.InvalidEmail:
                      return "The e-mail address provided is invalid. Please check the value and try again.";
  
                  case MembershipCreateStatus.InvalidAnswer:
                      return "The password retrieval answer provided is invalid. Please check the value and try again.";
  
                  case MembershipCreateStatus.InvalidQuestion:
                      return "The password retrieval question provided is invalid. Please check the value and try again.";
  
                  case MembershipCreateStatus.InvalidUserName:
                      return "The user name provided is invalid. Please check the value and try again.";
  
                  case MembershipCreateStatus.ProviderError:
                      return "The authentication provider returned an error. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
  
                  case MembershipCreateStatus.UserRejected:
                      return "The user creation request has been canceled. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
  
                  default:
                      return "An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
              }
          }
          #endregion
      }
  }
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/plrPE.png" alt="enter image description here"></p>