title: Asp.Net MVC: Custom AuthorizeAttribute filterContext.Result is HttpUnauthorizedResult = true on build
body: <p>I have a custom role provider with a custom AuthorizeAttribute that will redirect to an access denied page if they aren't a member of the particular role. When I build project from visual studio and open page(has AccessDeniedAuthorizeAttribute in action method)  in browser, <code>filterContext.Result is HttpUnauthorizedResult</code> is always true and it redirects to AccessDenied page. But on next refresh it works fine. Any ideas?</p>
  
  <pre><code>public class AccessDeniedAuthorizeAttribute : AuthorizeAttribute
      {
          public override void OnAuthorization(AuthorizationContext filterContext)
          {
              base.OnAuthorization(filterContext);
  
              if (filterContext.Result is HttpUnauthorizedResult) //Unauthorized request
              {
                //check if user is not logged in then redirect to login page
                if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
                {
                  filterContext.Result = new RedirectResult(System.Web.Security.FormsAuthentication.LoginUrl + "?returnUrl=" +
                  filterContext.HttpContext.Server.UrlEncode(filterContext.HttpContext.Request.RawUrl));
                }
                else //user is logged in but don't have required roles to access page
                {
                  filterContext.Controller.TempData["NeededRoles"] = base.Roles;
                  filterContext.Result = new RedirectResult("~/AccessDenied");
                }
              }
          }
      }
  </code></pre>