title: ASP.NET MVC 5 Identity 2 Login redirect based on user role
body: <p>I am trying to redirect user to page based on their role, </p>
  
  <p>This is the default implementation of the login function that come with ASP.NET MVC 5:</p>
  
  <pre><code>[HttpPost]
  [AllowAnonymous]
  [ValidateAntiForgeryToken]
  public async Task&lt;ActionResult&gt; Login(LoginViewModel model, string returnUrl)
  {
      if (ModelState.IsValid)
      {
          var user = await UserManager.FindAsync(model.UserName, model.Password);
          if (user != null)
          {
              await SignInAsync(user, model.RememberMe);
              return RedirectToLocal(returnUrl);
          }
          else
          {
              ModelState.AddModelError("", "Invalid username or password.");
          }
      }
  
      // If we got this far, something failed, redisplay form
      return View(model);
  }
  
  private ActionResult RedirectToLocal(string returnUrl)
  {
      if (Url.IsLocalUrl(returnUrl))
      {
          return Redirect(returnUrl);
      }
      else
      {
          return RedirectToAction("Index", "Employer");
      }
  }
  </code></pre>
  
  <p>I want to be able to redirect user based on their role, I tried to do it this way:</p>
  
  <pre><code>[HttpPost]
  [AllowAnonymous]
  [ValidateAntiForgeryToken]
  public async Task&lt;ActionResult&gt; Login(LoginViewModel model, string returnUrl)
  {
      if (ModelState.IsValid)
      {
          var user = await UserManager.FindAsync(model.UserName, model.Password);
          if (user != null)
          {
              await SignInAsync(user, model.RememberMe);
  
              //role Employer go to Employer page
              if (UserManager.IsInRole(user.Id, "Employer"))
              {
                  return RedirectToAction("Index", "Employer");
              }
              //role Admin go to Admin page
              else if (UserManager.IsInRole(user.Id, "Admin"))
              {
                  return RedirectToAction("Index", "Admin");
              }
              else
              {
                  //no role
                  return RedirectToAction("Index", "Home");
              }
          }
          else
          {
              ModelState.AddModelError("", "Invalid username or password.");
          }
      }
  
      // If we got this far, something failed, redisplay form
      return View(model);
  }
  </code></pre>
  
  <p>But there is a problem, although the site is redirecting me to the correct page,  If i navigate by typing in the url foo.com/admin when i am not logged in with an admin account, the site brings me to the login page with url foo.com/Account/Login?ReturnUrl=%2Fadmin, which is the expected behavior. </p>
  
  <p>if i login with an employer account at this point, it will redirect me to the employer's page and log me in as an employer, which isn't wrong, but that shouldn't be the case, the site should mention I should login with an admin account instead because the return url is "admin". I hope i am making sense.</p>