title: Override the User.IsInRole and [Authorize(Roles = "Admin")] for MVC4 application
body: <p>I have created a custom role provider for my MVC4 application where I have been successfully able to override CreateRole, GetAllRoles and RoleExists methods and link them to my existing database as follows:</p>
  
  <pre><code>namespace Project.Providers
  {
    public class MyProvider : System.Web.Security.SqlRoleProvider
    {
      private MyContext dbcontext = new MyContext(System.Configuration.ConfigurationManager.ConnectionStrings["MyContext"].ConnectionString);
      private Repository&lt;MyUser&gt; userRepository;
      private Repository&lt;Role&gt; roleRepository;
  
      public MyProvider()
      {
          this.userRepository = new Repository&lt;MyUser&gt;(dbcontext);
          this.roleRepository = new Repository&lt;Role&gt;(dbcontext);
      }
  
      public override string[] GetAllRoles()
      {
          IEnumerable&lt;Role&gt; dbRoles = roleRepository.GetAll();
          int dbRolesCount = roleRepository.GetAll().Count();
          string[] roles = new string[dbRolesCount];
          int i = 0;
          foreach(var role in dbRoles)
          {
              roles[i] = role.Name;
              i++;
          }
          return roles;
      }
  
      public override bool RoleExists(string roleName)
      {
          string[] roles = { "Admin", "User", "Business" };
          if(roles.Contains(roleName))
              return true;
          else
              return false;
      }
  
      public override void CreateRole(string roleName)
      {
          Role newRole = new Role();
          newRole.Name = roleName;
          roleRepository.Add(newRole);
          roleRepository.SaveChanges();
      }
  
      public override bool IsUserInRole(string userName, string roleName)
      {
          MyUser user = userRepository.Get(u =&gt; u.Username == userName).FirstOrDefault();
          Role role = roleRepository.Get(r =&gt; r.Name == roleName).FirstOrDefault();
          if (user.RoleID == role.RoleID)
              return true;
          else
              return false;
      }
    }
  }
  </code></pre>
  
  <p>I have been unable to find a way to override the </p>
  
  <pre><code>User.IsInRole(string roleName)
  </code></pre>
  
  <p>What else must I do so that When I use:</p>
  
  <pre><code>[Authorize(Roles = "Admin")]
  </code></pre>
  
  <p>It will be based on the role provider that I have set up and not the asp default.</p>
  
  <p>My user class is now as follows:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.ComponentModel.DataAnnotations;
  using System.Collections;
  using System.Security.Principal;
  
  namespace Project.Data
  {
    public class MyUser : IPrincipal
    {
      [Key]
      public int UserID { get; set; }
  
      [StringLength(128)]
      public string Username { get; set; }              
  
      .....other properties
  
      public IIdentity Identity { get; set; }
  
      public bool IsInRole(string role)
      {
          if (this.Role.Name == role)
          {
              return true;
          }
          return false;
      }
  
      public IIdentity Identity
      {
          get { throw new NotImplementedException(); }
      }
    }
  }
  </code></pre>
  
  <p>My stack trace seems to be following over at: </p>
  
  <pre><code>System.Web.Security.RolePrincipal.IsInRole(String role) 
  </code></pre>
  
  <p>So I have tried to implement a custom RolePrincipal in the same manner I set the custom Provider any ideas how I can do this? Not sure what constructor params it takes. Here is my attempt:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Configuration.Provider;
  using Project.Data;
  using System.Web.Security;
  using System.Security.Principal.IIdentity;
  
  namespace Project.Principal
  {
    public class MyPrincipal : System.Web.Security.RolePrincipal
    {
      private MyContext dbcontext = new MyContext(System.Configuration.ConfigurationManager.ConnectionStrings["MyContext"].ConnectionString);
      private Repository&lt;MyUser&gt; userRepository;
      private Repository&lt;Role&gt; roleRepository;        
  
      public MyPrincipal()
      {
          this.userRepository = new Repository&lt;MyUser&gt;(dbcontext);
          this.roleRepository = new Repository&lt;Role&gt;(dbcontext);
      }
  
      public override bool IsInRole(string role)
      {
          //code to be added
          return true;
      }
  }
  </code></pre>
  
  <p>}</p>