Last active
August 29, 2015 14:06
-
-
Save cerkit/79957452f1bf12dfe06d to your computer and use it in GitHub Desktop.
BasicAuthenticationForXMLHttpRequests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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