title: ASP.NET MVC 5 Custom Role Provider Not Working?
body: <p>i have a problem with my custom role providor "string[] GetRolesForUser(string username)" method but the proccess dont go through it. My code is:</p>
  
  <p><strong>Controller (Just a piece of it):</strong></p>
  
  <pre><code>[Authorize(Roles="Administrator")]
  public class UsersController : AdminBaseController
  {
      private IUsersRepository users;
      private IDepartmentsRepository departments;
  
      public UsersController()
      {
          this.users = new UsersRepository(new TicketsContext());
          this.departments = new DepartmentsRepository(new TicketsContext());
      }
  }
  </code></pre>
  
  <p><strong>Custom Role Provider:</strong></p>
  
  <pre><code>public class CustomRoleProvider : RoleProvider
  {
      public override bool IsUserInRole(string username, string roleName)
      {
          var userRoles = GetRolesForUser(username);
          return userRoles.Contains(roleName);
      }
  
      public override string[] GetRolesForUser(string username)
      {
          //Return if the user is not authenticated
          if (!HttpContext.Current.User.Identity.IsAuthenticated)
              return null;
  
          //Return if present in Cache
          var cacheKey = string.Format("UserRoles_{0}", username);
          if (HttpRuntime.Cache[cacheKey] != null)
              return (string[])HttpRuntime.Cache[cacheKey];
  
          //Get the roles from DB
          var userRoles = new string[] { };
          var user = db.Users.Where(u =&gt; u.email == username).FirstOrDefault();
          if (user != null)
          {
              if(user.access_level == 0)
              {
                  userRoles = new[] { "Administrator" };
              }
              else
              {
                  userRoles = new[] { "Normal" };
              }
          }
  
          //Store in cache
          HttpRuntime.Cache.Insert(cacheKey, userRoles, null, DateTime.Now.AddMinutes(_cacheTimeoutInMinutes), Cache.NoSlidingExpiration);
  
          // Return
          return userRoles.ToArray();
      }
  }
  </code></pre>
  
  <p><strong>Web.config</strong></p>
  
  <pre><code>&lt;!-- Custom Role Provider --&gt;
  &lt;roleManager enabled="true" defaultProvider="TicketsRoleProvider"&gt;
    &lt;providers&gt;
      &lt;add name="TicketsRoleProvider"
           type="Tickets.CustomRoleProvider"
           cacheTimeoutInMinutes="30" /&gt;
    &lt;/providers&gt;
  &lt;/roleManager&gt;
  </code></pre>
  
  <p>I cant get it to work, and i dont know why.</p>
  
  <p>Can anyone help me pls ?</p>
  
  <p>Thanks</p>