Thursday, June 30, 2016

VS Item Template for ASP.NET Identity 2.0

ASP.NET Identity does many cool things; a lot more than the old Membership Provider did. I did a project lately that needed to do what the Membership Provider did, Users and Roles. It turns out that the ASP.NET Identity supports Membership, but it is poorly documented.

I looked high and low on the Internet for good examples of ASP.NET Identity doing simple Membership without any extra fluff (my client didn’t want any of it), but did not find any! Since I could not find a good any, I decided to make one and put it on GitHub.

I also built a Visual Studio Item Template that will add most of what you need add and edit roles and users.

The Template

I released my template on my Github account here. It only works with ASP.NET MVC projects created in Visual Studio 2015. It is too stupid to validate that you are using To use my template:

  1. Download FormsAuth.zip from the latest release.
  2. Copy it to <My Documents>\Visual Studio 2015\Templates\Item Templates\Visual C#
  3. Open Developer Command Prompt for VS2015 as administrator
  4. Run devenv /installvstemplates
  5. Open Visual Studio 2015 (don’t use an already opened copy, the template won’t be there)
  6. Create a new project (you can open an existing project as long as it is an ASP.NET MVC project created in VS 2015 or upgraded to ASP.NET Identity 2, VS 2013 projects won’t work, then skip to step 9)
  7. Choose ASP.NET Web Application, name it and click OK
  8. Select the template MVC
  9. Project => Add New Item (or Ctrl+Shift+A)
  10. On the left, select Visual C# then select Forms Auth Role and User CRUD for MVC Project using ASP.NET Identity (better name?)
  11. Follow the instructions in the read me file.

The things I make you do

I wanted to be safe and not touch any of your code, so I am asking you to do it for me.

Create ApplicationRoleManager

The default Visual Studio ASP.NET MVC project is aware of Users but not Roles. You will need to tell it to care about Roels by adding an ApplicationRoleManager. So go to App_Start > Startup.Auth.cs and change the Startup.ConfigureAuth to look like this:

public partial class Startup
{
  
public void ConfigureAuth(IAppBuilder app)
   {
       app.CreatePerOwinContext(
ApplicationDbContext.Create);
      
// Add this line:
       app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);
       app.CreatePerOwinContext<
ApplicationUserManager>(ApplicationUserManager.Create);
       app.CreatePerOwinContext<
ApplicationSignInManager>(ApplicationSignInManager.Create);

Add links to navigate to User and Roles pages

OK, you have Users and Roles and forms to edit Users and Roles; but how do you get to them? If you are using the default ASP.NET MVC layout, make the main menu in _Layout.cshtml look like this:

<div class="navbar-collapse collapse">
   <ul class="nav navbar-nav">
       <li>@Html.ActionLink("Home", "Index", "Home")</li>
       <li>@Html.ActionLink("About", "About", "Home")</li>
       <li>@Html.ActionLink("Contact", "Contact", "Home")</li>

       @if (Request.IsAuthenticated && User.IsInRole("Admin")) {
          
<li>@Html.ActionLink("RolesAdmin", "Index", "RolesAdmin")</li>
           <li>@Html.ActionLink("UsersAdmin", "Index", "UsersAdmin")</li>
       }
  
</ul>
   @Html.Partial("_LoginPartial")
</div>

Create “Seed” User

OK, you have all this wonderful User and Role functionality. The trouble is that you need a user who is in the role “Admin” to add and edit users. I added a special form that will allow you to register one and only one “seed” user who will be a member of “Admin”; if it sees that the role “Admin” already exists, it will send you to an error page. Go to http://yourdomain/seeduser/register and create a your new very first Admin user! After you create your seed user, you can delete SeedUserController and the SeedUser Views (files in /Views/SeedUsers).

Other things to do

If you don’t want to expose any of your site to the outside, make all controllers require authorization with the [Authorize] attribute.

Delete Register page and routes (Including my Register Seed User). In the AccountController, delete (or comment out) both Register methods.