I have a jquery ajax call that needs to pass an auth header. I’ve tried several different approaches. But my ajax call currently looks like this…
$.ajax({
headers: {"Authorization": "Basic YgBhAHMAaQBjACAAbAAxAHQAdABsADMAcAAxAGcAbAAxAHQAdABsADMAcAAxAGcAOgBsADMAKwBtADMAMQBuAA=="},
url: fetchurl,
type: 'GET',
dataType: 'json',
async: false,
contentType: 'application/json; charset=utf-8',
success: function (data) {
$.each(data, function (key, val) {
$('<option/>').attr('value', val.ProductCd)
.html(val.ProductCd + " - " + val.Description)
.appendTo(productselect);
});
$.unblockUI();
},
error: function (error, textStatus, errorThrown) {
alert(textStatus);
}
});
I thought my decoding problems might have to do with who was doing the encoding, so I generated that token via the C# class and pasted it into my script code.
I’ve also tried using plugins for encoding in jquery and using the beforesend callback to set the header.
Here I try to decode:
string authToken = actionContext.Request.Headers.Authorization.Parameter;
string decodedToken = Encoding.UTF8.GetString(Convert.FromBase64String(authToken));
string username = decodedToken.Substring(0, decodedToken.IndexOf(":"));
string password = decodedToken.Substring(decodedToken.IndexOf(":") + 1);
My result is pretty much always the same.
After decoding the auth header I get garbage:
“�w0u�\n�ZXNTLMSSP\0\0\0\0\0\0\0\0X\0\0\0\0\0\0\0X\0\0\0\0\0\0\0X\0\0\0\0\0\0\0X\0\0\0\0\0\0\0X\0\0\0\0\0\0\0X\0\0\0��\0\0\0I���ۛ��l�Y��#��\0\0\0��P-\”��\0\0\0\0”
OK. It took some hacking but I finally figured out that while I was sending a basic auth from my ajax call, the web api layer was receiving a negotiate auth. So I went into IIS disabled all auth on the web service save for anonymous. And then voila.