title: checking user's role during login using aspnet identity
body: <p>I am working on asp.net application. I have aspnetroles table with 4 roles:</p>
  
  <pre><code>**Id    Name**
  1   Admin
  4   Consumer
  2   Provider
  3   SaleUser
  </code></pre>
  
  <p>In AccountController Register Action method, I have added this:</p>
  
  <pre><code> var user = new ApplicationUser { UserName = model.Email, Email = model.Email};
                  var result = await UserManager.CreateAsync(user, model.Password);
                  **UserManager.AddToRole(user.Id, model.UserRole);**
  </code></pre>
  
  <p>Now I am checking while login in login action result like this:</p>
  
  <p>I see that aspnetusers and aspnetuseroles tables have correct data.</p>
  
  <pre><code>var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
  
  
     if (User.IsInRole(model.UserRole))
                              return RedirectToLocal(returnUrl);
  </code></pre>
  
  <p>but condition fails. How can I check if user belongs to specific role.</p>
  
  <p>I have added following line in Startup.cs ConfigureAuth method:</p>
  
  <pre><code> app.CreatePerOwinContext&lt;ApplicationRoleManager&gt;(ApplicationRoleManager.Create);
  </code></pre>
  
  <p>and this class in identityConfig class:</p>
  
  <pre><code> public class ApplicationRoleManager : RoleManager&lt;IdentityRole&gt;
      {
          public ApplicationRoleManager(IRoleStore&lt;IdentityRole, string&gt; store) : base(store)
          {
          }
          public static ApplicationRoleManager Create(IdentityFactoryOptions&lt;ApplicationRoleManager&gt; options, IOwinContext context)
          {
              var roleStore = new RoleStore&lt;IdentityRole&gt;(context.Get&lt;ApplicationDbContext&gt;());
              return new ApplicationRoleManager(roleStore);
          }
      }
  </code></pre>
  
  <p>and this code in accountcontroller:</p>
  
  <pre><code>  private ApplicationRoleManager roleManager;
          public ApplicationRoleManager RoleManager
          {
              get
              {
                  return this.roleManager ?? HttpContext.GetOwinContext().Get&lt;ApplicationRoleManager&gt;();
              }
              private set { this.roleManager = value; }
          }
  </code></pre>
  
  <p>but still same issue</p>
  
  <p><strong>Update</strong></p>
  
  <pre><code>var user = new ClaimsPrincipal(AuthenticationManager.AuthenticationResponseGrant.Identity);
  
  if (User.IsInRole(model.UserRole))
      return RedirectToLocal(returnUrl);
  else
  {
      AuthenticationManager.AuthenticationResponseGrant = null;
  
      model.Roles = GetRoles();
      ModelState.AddModelError("", "You cannot login as " + model.UserRole);
      return View(model);
  }
  </code></pre>