title: Custom role provider in ASP MVC
body: <p>Hello i am kinda at lost here and i don't know what to fix:</p>
  
  <p>The issue is that, my application still doesn't detect the <strong>roles</strong> assigned.
  Like if i have something like
  <code>[Authorize(Roles="user")]</code>. It won't detect my login and won't allow me to proceed on that view.</p>
  
  <p>I've been following a bad tutorial and here's what i have to so far:
  (i want to make it first at least before i move on to another one)</p>
  
  <p>Here are my database tables:</p>
  
  <p>I didn't put any hash yet for simplicity:</p>
  
  <p><strong>Login table</strong></p>
  
  <p><a href="https://i.stack.imgur.com/5iIwF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5iIwF.png" alt="enter image description here"></a></p>
  
  <p><strong>roles table</strong></p>
  
  <p><a href="https://i.stack.imgur.com/l3WnQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/l3WnQ.png" alt="enter image description here"></a></p>
  
  <p>I use <code>inner join</code> and i will have something like this</p>
  
  <p><code>select A.username, A.role from login A INNER JOIN roles B on A.role = B.id</code></p>
  
  <p><a href="https://i.stack.imgur.com/sn3xA.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/sn3xA.png" alt="enter image description here"></a></p>
  
  <p>I use code first EF 4.0 and here are my code snippets:</p>
  
  <p>I have a class <code>roleprovider</code> which inherits from <code>roleprovider</code>.</p>
  
  <p>I only implemented 2 methods from it, namely: <code>GetRolesForUser</code> and <code>IsUserInRole</code></p>
  
  <p><strong>GetRolesForUser</strong></p>
  
  <pre><code>public override string[] GetRolesForUser(string uname)
      {
          if (!HttpContext.Current.User.Identity.IsAuthenticated)
          {
              return null;
          }
  
          var cacheKey = string.Format("{0_role}", uname);
          if (HttpRuntime.Cache[cacheKey] != null)
          {
              return (string[])HttpRuntime.Cache[cacheKey];
          }
  
          string[] roles = new string[] { };
          using (EmployeeContext emp = new EmployeeContext())
          {
              roles = (from a in emp.login
                       join b in emp.roles on a.role equals b.id
                       where a.username.Equals(uname)
                       select b.role).ToArray&lt;string&gt;();
              if (roles.Count() &gt; 0)
              {
                  HttpRuntime.Cache.Insert(cacheKey, roles, null, DateTime.Now.AddMinutes(_cacheTimeoutInMinute), Cache.NoSlidingExpiration);
              }
          }
          return roles;
      }
  </code></pre>
  
  <p><strong>IsUserInRole</strong></p>
  
  <pre><code>public override bool IsUserInRole(string uname, string roleName)
  {
       var userRoles = GetRolesForUser(uname);
       return userRoles.Contains(roleName);
  }
  </code></pre>
  
  <p><strong>Web.Config</strong></p>
  
  <pre><code>&lt;roleManager&gt;
    &lt;providers&gt;
      &lt;clear/&gt;
      &lt;add name="roleprovider"  type="MvcApplication6.Helper.roleprovider"/&gt;
    &lt;/providers&gt;
  &lt;/roleManager&gt;
  </code></pre>
  
  <p></p>
  
  <p>I apologize if i can't explain the code properly as of now because i am still in the process of learning it. My primary agenda as of now i to make the code work first but i am somehow lost because i am not particularly sure of what i am missing.</p>
  
  <p>Recap of the issue:
  -Application won't detect the roles from the database and won't let me proceed if i try logging in.</p>
  
  <p>edit: here is my login code( i have authentication mode implemented )</p>
  
  <pre><code>   [HttpGet]
      [ActionName("login")]
      public ActionResult login_load()
      {
          return View();
      }
  
      [HttpPost]
      [ActionName("login")]
  
      public ActionResult login_post(string uname,string pword)
      {
          using (EmployeeContext emp = new EmployeeContext())
          {
  
              int success = emp.login.Where(x =&gt; x.username == uname &amp;&amp; x.password == pword).Count();
              if (success == 1)
              {
                  FormsAuthentication.SetAuthCookie(uname, false);
  
                  return RedirectToAction("Details", "Enrollment");
              }
              return View();
          }
      }
  </code></pre>