title: ASP.NET MVC5 Identity Implementation
body: <p>I've been working on a MVC4 EF6 web application project which uses simple membership for web security and I wanted some users to have access to some webpages and restrictions to others. I've just found out that MVC5 offers EntityFrameWork.Identity which does what I want [Authorize(Roles=admin)]. So I started a MVC 5 project and copied over my Models,Context,Views and Viewmodels and everything seems to be working the same.</p>
  
  <p>I read online that I need to change my User class to derive from Identity user to support UserRoles etc. </p>
  
  <p>Since my original User class uses <code>public bool IsAdministrator { get; set; }</code> to differentiate from Admins and Users but Identity offers you a AspNetUserRoles table to do it. What steps do I need to do so that I can use <code>[Authorize(Roles=admin)]</code> to restrict certain controllers to certain users? I've been following <a href="http://johnatten.com/2014/06/22/asp-net-identity-2-0-customizing-users-and-roles/" rel="nofollow">http://johnatten.com/2014/06/22/asp-net-identity-2-0-customizing-users-and-roles/</a> but all the application manager, DBcontext configuration,Claims and Stores are so confusing to me. </p>
  
  <p>IdentityModels.cs</p>
  
  <pre><code>public class ApplicationUser : IdentityUser
  {        public async Task&lt;ClaimsIdentity&gt; GenerateUserIdentityAsync(UserManager&lt;ApplicationUser&gt; manager)
      {
          // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
          var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
          // Add custom user claims here
          return userIdentity;
      }
  
      public int UserID { get; set; }
  
      public bool IsAdministrator { get; set; }
      [StringLength(50, MinimumLength = 1)]
      public string LastName { get; set; }
      [StringLength(50, MinimumLength = 1, ErrorMessage = "First name cannot be longer than 50 characters.")]
  
      [Column("FirstName")]
      public string FirstMidName { get; set; }
  
      public string FullName
      {
          get { return FirstMidName + " " + LastName; }
      }
      [DataType(DataType.Date)]
      [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
      public DateTime EnrollmentDate { get; set; }
  
      public int DepartmentID { get; set; }
      [ForeignKey("DepartmentID")]
      public virtual Department Department { get; set; }
      public int DepotID { get; set; }
      [ForeignKey("DepotID")]
      public virtual Depot Depot { get; set; }
      public virtual ICollection&lt;Ticket&gt; Tickets { get; set; }
  
  }
  </code></pre>
  
  <p>Ticket.cs </p>
  
  <pre><code>public enum Priority
  {
      Low, Med, High
  }
  public class Ticket
  {
      public int? TicketID { get; set; }
      [Required(ErrorMessage = "Please enter the description")]
      public string Issue { get; set; }
      [Display(Name = "Administrator")]
      [Required(ErrorMessage = "Please select the Administrator")]
      public int IssuedTo { get; set; }
      public int Author { get; set; }
  
      [DisplayFormat(NullDisplayText = "No Priority")]
      public Priority Priority { get; set; }
      [ForeignKey("CategoryID")]
      public virtual Category Category { get; set; }
      public int CategoryID { get; set; }
      public int UserID { get; set; }
      [ForeignKey("UserID")]
      public virtual User User { get; set; }
  }
  </code></pre>
  
  <p>Depot.cs</p>
  
  <pre><code>public class Depot
  {
      public int DepotID { get; set; }
      [StringLength(50, MinimumLength = 1)]
      public string DepotName { get; set; }
      public virtual ICollection&lt;User&gt; Users { get; set; }
  
  }
  </code></pre>
  
  <p>Department.cs </p>
  
  <pre><code>public class Department
  {
  
      public int DepartmentID { get; set; }
  
      [StringLength(50, MinimumLength = 1)]
      public string DepartmentName { get; set; }
  
      public virtual ICollection&lt;User&gt; Users { get; set; }
  }
  </code></pre>
  
  <p>Category.cs</p>
  
  <pre><code>public class Category
  {
      [DatabaseGenerated(DatabaseGeneratedOption.None)]
      public int CategoryID { get; set; }
      public string CategoryName { get; set; }
      public virtual ICollection&lt;Ticket&gt; Tickets { get; set; }
  }
  </code></pre>
  
  <p>IssueContext(dbcontext)</p>
  
  <pre><code>public class IssueContext : DbContext
  {
      public DbSet&lt;User&gt; Users { get; set; }
      public DbSet&lt;Ticket&gt; Tickets { get; set; }
      public DbSet&lt;Category&gt; Categories { get; set; }
      public DbSet&lt;Department&gt; Departments { get; set; }
      public DbSet&lt;Depot&gt; Depots { get; set; }
  
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          modelBuilder.Conventions.Remove&lt;PluralizingTableNameConvention&gt;();
  
      }
  }
  </code></pre>
  
  <p>ApplicationContext in IdentityModel.cs</p>
  
  <pre><code>public class ApplicationDbContext : IdentityDbContext&lt;ApplicationUser&gt;
  {
      public ApplicationDbContext()
          : base("DefaultConnection", throwIfV1Schema: false)
      {
      }
  </code></pre>
  
  <p>Configuration.cs(Seed)</p>
  
  <pre><code>        var users = new List&lt;User&gt;
          {
              new User { FirstMidName = "Jason",   LastName = "Wan",
                  EnrollmentDate = DateTime.Parse("2016-02-18"), DepartmentID = 1, DepotID = 1,IsAdministrator = true},
              new User { FirstMidName = "Andy", LastName = "Domagas",
                  EnrollmentDate = DateTime.Parse("2016-02-18"), DepartmentID = 1,DepotID = 1,IsAdministrator = true},
              new User { FirstMidName = "Denis",   LastName = "Djohar",
                  EnrollmentDate = DateTime.Parse("2016-02-18"), DepartmentID = 1 ,DepotID = 1,IsAdministrator = true },
              new User { FirstMidName = "Christine",   LastName = "West",
                  EnrollmentDate = DateTime.Parse("2016-02-18"), DepartmentID = 2, DepotID = 3,IsAdministrator = false},
  
          };
          users.ForEach(s =&gt; context.Users.AddOrUpdate(p =&gt; p.FirstMidName, s));
          context.SaveChanges();
  
          users.ForEach(s =&gt; context.Users.AddOrUpdate(p =&gt; p.LastName, s));
          context.SaveChanges();
  </code></pre>