vpetkovic
2/3/2020 - 2:02 PM

CORS

Mainly needed when using Net Core API endpoint with windows authentication. In order to authenticate correctly couple of things needs to be done

Server

Before setting up anything in IIS 10, CORS needs to be enabled. To do so (Additionally CORS Extension might need to be installed)

  1. Open IIS Manager (Administrator)
  2. Select target site, and click "Feature View" tab shown at bottom on right side.
  3. Click on Directory Browsing option from IIS section.
  4. Click on "Enable" link on right side in actions window.
  5. Refresh site once.
  6. At site's physical path, you will find "web.config" generated for you.
  7. In that, add below code to enable CORS.

<system.webServer>
</system.webServer>

  1. Now, refresh and check if CORS working or not.
  2. Look at the CORS.js file to see how to setup AJAX Call
  3. If using CHROME or EDGE(ium) flags for CORS needs to be disabled
$(document).ready(function () {

    var settings = {
        "url": "[ENDPOINT]",
        "method": "GET",
        "crossDomain": true, // THIS LINE TO BE INLCUDED
        "xhrFields": { 
            withCredentials: true // THIS LINE TO BE INLCUDED
        },
    };

    $.ajax(settings)
        .done(function (response) {
            console.log(response)
        })

});

// Or use it as function helper to make calls easier and DRY
function APIHelper(url) {
    var settings = {
        "url": url,
        "method": "GET",
        "crossDomain": true,
        "xhrFields": {
            withCredentials: true
        },
    };

    return settings;
}