title: asp mvc 3 ActionFilter for basic authentication
body: <p>I have an ASP MVC3 restful service that uses basic authentication.  After searching stack overflow, I created the following code.</p>
  
  <pre><code>public class BasicAuthentication : ActionFilterAttribute
  {
      public override void OnActionExecuting(ActionExecutingContext filterContext)
      {
          var req = filterContext.HttpContext.Request;
          if (String.IsNullOrEmpty(req.Headers["Authorization"]))
          {
              filterContext.Result = new HttpNotFoundResult();
          }
          else
          {
              var credentials = System.Text.ASCIIEncoding.ASCII
                          .GetString(Convert.FromBase64String(req.Headers["Authorization"].Substring(6)))
                          .Split(':');
              var user = new { Name = credentials[0], Password = credentials[1] };
              if(!(user.Name == "username" &amp;&amp; user.Password == "passwords"))
              {
                  filterContext.Result = new HttpNotFoundResult();
              }
          }
      }
  }
  </code></pre>
  
  <p>1) Is an ActionFilterAttribute the best way to do this?</p>
  
  <p>2) Is setting filterContext.Result the correct way to deny access to the controller method?</p>
  
  <p>3) Is there anything I'm doing wrong?</p>
  
  <p>Thanks.</p>
  
  <p>-Nick</p>