Ajax Call in Basic Authentication RRS feed

  • Question

  • User53449276 posted

    Hi, I created an autocomplete() ajax call to return Json data from my controller to the View. But the application is protected in Basic Authentication folder. User is required to login using their domain username and password. So I got 401 error when calling controller action. I searched online and found solution about beforeSend but I don't know how to use it in my function.  Thanks. Below is my ajax code:

    $(document).ready(function () {
        beforeSend: function(xhr) {
            xhr.setRequestHeader("Authorization", "Basic " + btoa(username + ":" + password));
        //load expertises
            source: function (request, response) {
                    url: "/Home/GetExpertiseList",
                    type: "POST",        
                    dataType: "json",
                    data: { searchstr: request.term },
                    success: function (data) {
                            function (item) {
                                return { value: item};
            messages: {
                noResults: "", results: ""

    Tuesday, August 14, 2018 3:22 PM

All replies

  • User475983607 posted

    I'm not sure what you need help with. 

    If the AJAX function actually works correctly, you should be able to replace the username and password variable with the actual Basic auth username and password. That should allow the request to authenticate.   Have you tried that?

    You'll want to do a username and password validation step.  If the username and password are valid then store the values in local storage so that the app is a bit more secure than having credentials in plain text - base64 encoded or not.

    It might help if you learn Basic Authentication if you are not familiar with Basic Auth.


    Tuesday, August 14, 2018 4:26 PM
  • User53449276 posted

    I am not good at Jquery. How do I retrieve user's windows username and password?

    Tuesday, August 14, 2018 5:04 PM
  • User475983607 posted

    I am not good at Jquery. 

    Then you should practice and read the reference documentation. 


    How do I retrieve user's windows username and password?

    Basic Authentication and Windows Authentication are not the same.   My previous link explains Basic Authentication.  The following link explains Windows Authentication. 


    Windows Authentication should be transparent if you are using IE.  If not, then see your system admin for assistance. 

    Tuesday, August 14, 2018 5:20 PM