public static void UpdateStatusCodeForFailedAuthentication() { HttpContext httpContext = HttpContext.Current; if (httpContext != null && httpContext.Response.StatusCode == (int)HttpStatusCode.Found) { HttpRequestWrapper request = new HttpRequestWrapper(httpContext.Request); if (!request.IsBrowserRequest()) { httpContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized; httpContext.Response.ClearContent(); try { if (httpContext.Response.Headers.AllKeys.Contains("Location")) { httpContext.Response.Headers.Remove("Location"); } } catch (PlatformNotSupportedException) { // We need IIS 7 integrated mode to remove the header. Just swallow the exception if // we can't remove the header. } } } }