Skip to content

Instantly share code, notes, and snippets.

@cerkit
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cerkit/79957452f1bf12dfe06d to your computer and use it in GitHub Desktop.
Save cerkit/79957452f1bf12dfe06d to your computer and use it in GitHub Desktop.
BasicAuthenticationForXMLHttpRequests
// If the request was unauthorized, add the WWW-Authenticate header
// to the response.
private static void OnApplicationEndRequest(object sender, EventArgs e)
{
var response = HttpContext.Current.Response;
// see if the request sent an X-Requested-With header (Non-Browser request -
// used by jQuery and Angular implementations to prevent the browser from
// presenting the default Login dialog)
var request = HttpContext.Current.Request;
string authType = "Basic";
if (response.StatusCode == 401)
{
if (request.Headers.AllKeys.Contains("X-Requested-With"))
{
if(request.Headers["X-Requested-With"] == "XMLHttpRequest")
{
authType = "xBasic";
}
}
response.Headers.Add("WWW-Authenticate",
string.Format("{0} realm=\"{1}\"", authType, Realm));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment