I'm using jQuery to call a .Net web service like this:
var service_url = "https://example.com/myservice.asmx" $.ajax({ type: "GET", url: service_url, dataType: "xml", data: "ParamId=" + FormId.value, processData: false, error: function(XMLHttpRequest, textStatus, errorThrown) { ajaxError(XMLHttpRequest, textStatus, errorThrown); }, success: function(xml) { DoSomething(xml); } });
Now I want to wrap "https://example.com/myservice.asmx" in Windows Authentication. How can I pass credentials to the service using jQuery/javascript?
Ideally I'd like to use the current user's credentials but if needed I can use 1 master credential for all service calls.
Windows authentication authenticates the user by validating the credentials against the user account in a Windows domain. Basic authentication verifies the credentials that are provided in a form against the user account that is stored in a database.
I think nowadays you can just set the withCredentials
property of the request object to true
, e.g.:
$.ajax({ type: "GET", url: service_url, dataType: "xml", data: "ParamId=" + FormId.value, processData: false, xhrFields: { withCredentials: true }, error: function(XMLHttpRequest, textStatus, errorThrown) { ajaxError(XMLHttpRequest, textStatus, errorThrown); }, success: function(xml) { DoSomething(xml); } });
That causes existing authentication headers/cookies to be passed along in the AJAX request, works for me. No need to do your own Base encoding, etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With