title: How to override HandleUnauthorizedRequest in ASP.NET Core
body: <p>I'm migrating my project to asp.net core and I'm stuck in migrating my CustomAuthorization attribute for my controllers. Here is my code.</p>
  
  <pre><code>public class CustomAuthorization : AuthorizeAttribute
  {
      public string Url { get; set; }
  
      protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
      {
          if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
          {
              filterContext.Result = new RedirectResult(Url + "?returnUrl=" + filterContext.HttpContext.Request.Url.PathAndQuery);
          }
          else if (!Roles.Split(',').Any(filterContext.HttpContext.User.IsInRole))
          {
              filterContext.Result = new ViewResult
              {
                  ViewName = "AcessDenied"
              };
          }
          else
          {
              base.HandleUnauthorizedRequest(filterContext);
          }
      }
  }
  </code></pre>
  
  <p>then i used it to my controllers </p>
  
  <pre><code>[CustomAuthorization(Url = "/Admin/Account/Login", Roles = "Admin")]
  public abstract class AdminController : Controller { }
  </code></pre>
  
  <p>so, basically i can use it to redirect to different login page when roles is not met. I have few areas and each of them have different login page. I tried using the <strong>CookieAuthenticationOptions</strong> like this</p>
  
  <pre><code>services.Configure&lt;CookieAuthenticationOptions&gt;(options =&gt;
  {
      options.AuthenticationScheme = "Admin";
      options.LoginPath = "/Admin/Account/Login";
  });
  </code></pre>
  
  <p>then on my admin controller </p>
  
  <pre><code>[Area("Admin")]
  [Authorize(ActiveAuthenticationSchemes = "Admin", Roles = "Admin")]
  </code></pre>
  
  <p>but after i login, it still cant get in.</p>