title: How do you define the login page you want unauthorized user to be redirected to
body: <p>I have decorated my controller with an Authorize attribute, as so:</p>
  
  <pre><code>[Authorize(Roles="ExecAdmin")]
  </code></pre>
  
  <p>If I try to go to that controller after logging in as a user who is not ExecAdmin, it does appear to be attempting to redirect to a login page. BUT, the page it is attempting to redirect to is not my login page, it is a view called LogOnUserControl.ascx. This is a partial view that is not displayed by my login page.</p>
  
  <p>I have no idea why it is doing this -- or maybe it is trying to redirect to some other page altogether, one which does display LogOnUserControl.ascx. Or maybe it is looking for anything with "LogOn" in the name? (Though the name of my login view is LogOn.aspx...)</p>
  
  <p>How can I tell it what page to redirect to?</p>
  
  <p>UPDATE: I do have this in the global.asax</p>
  
  <pre><code>protected void Application_AuthenticateRequest(Object sender, EventArgs e)
  {
      HttpCookie authCookie = Context.Request.Cookies[FormsAuthentication.FormsCookieName];
      if (authCookie == null || authCookie.Value == "")
      {
          return;
      }
      FormsAuthenticationTicket authTicket = null;
      try
      {
          authTicket = FormsAuthentication.Decrypt(authCookie.Value);
      }
      catch
      {
          return;
      }
      string[] roles = authTicket.UserData.Split(new char[] { ';' });
      //Context.ClearError(); 
      if (Context.User != null)
      {
          Context.User = new System.Security.Principal.GenericPrincipal(Context.User.Identity, roles);
      }
  }
  </code></pre>
  
  <p>... since I am using a non-standard way of defining roles; i.e., I am not using ASP.NET membership scheme (with role providers defined in web.config, etc.). Instead I am setting roles this way:</p>
  
  <pre><code>// get user's role
  string role = rc.rolesRepository.GetUserType(rc.loginRepository.GetUserID(userName)).ToString();
  
  // create encryption cookie
  FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
          1,
          userName,
          DateTime.Now,
          DateTime.Now.AddMinutes(120),
          createPersistentCookie,
          role //user's role 
          );
  
  // add cookie to response stream
  string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
  
  System.Web.HttpCookie authCookie = new System.Web.HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
  System.Web.HttpContext.Current.Response.Cookies.Add(authCookie);
  </code></pre>
  
  <p>(This is called after the user has been validated.)</p>
  
  <p>Not sure how this could be impacting the whole thing, though ...</p>
  
  <p>UPDATE: Thanks to Robert's solution, here's how I solved it -- extend AuthorizeAttribute class:</p>
  
  <pre><code>public class AuthorizeAttributeWithMessage : AuthorizeAttribute
  {
      private string _message = "";
      public string Message
      {
          get { 
              return _message; 
          }
          set { 
              _message = value;
          }
      }
  
      protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
      {
          if (filterContext.HttpContext.Request.IsAuthenticated)
          {
              // user is logged in but wrong role or user:
              filterContext.Controller.TempData.Add("Message", Message);
          }
          base.HandleUnauthorizedRequest(filterContext);
      }
  }
  </code></pre>
  
  <p>Then in the LogOn view:</p>
  
  <pre><code>&lt;% 
      if (HttpContext.Current.Request.IsAuthenticated)
      {
          // authenticated users should not be here
          Response.Redirect("/Home/Index");
      }
  %&gt;
  </code></pre>
  
  <p>And in the home page view:</p>
  
  <pre><code>&lt;% if (TempData != null &amp;&amp; TempData.Count &gt; 0 &amp;&amp; TempData.ContainsKey("Message"))
     { %&gt;
  &lt;div class="largewarningtext"&gt;&lt;%= TempData["Message"]%&gt;&lt;/div&gt;
  &lt;% } %&gt;
  </code></pre>
  
  <p>And atop the affected controllers:</p>
  
  <pre><code>[AuthorizeAttributeWithMessage(Roles = "Consultant,ExecAdmin", Message = "You do not have access to the requested page")]
  </code></pre>
  
  <p>This has the advantage of ALWAYS redirecting any authenticated user who ends up on Logon.aspx -- authenticated users should not be there. If there is a message in the TempData, it will print it out on the home page; if not, it will at least have done the redirect.</p>