0

I want to access a SSL enabled WCF service from asp.net 2.0 application. Site was hosted on IIS 7.

Here is the Code

function getCourse() {
    $.ajax({
        type: "GET",
        url: "https://website.com/service.svc/task",
        processData: false,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        timeout: 7000,
        username: "someusername",
        password: "somepassword",
        success: function (data) { alert("success"); },
        error: function (xhr, status, error) { alert("error"); }
    });
};

I get 401 error, I didn't do any configuration in iis or webconfig related to the ssl access? Am I missing anything here? or the fact that I'm using asp.net 2.0 application cause the error?

HardCode
  • 2,025
  • 4
  • 33
  • 55

1 Answers1

0

Well I think you might need to add additional header in your jquery ajax function, like following one:

headers: { 
    Authenticate: Basic username:password (to base64)
}

or

beforeSend: setHeaders

function setHeader(xhr) {
    xhr.setRequestHeader('Authorization', 'Basic base64.encode(username:password)');
}

According to Wiki, 401 Unauthorized is defined as

Similar to 403 Forbidden, but specifically for use when authentication is required and has failed or has not yet been provided.

The response must include a WWW-Authenticate header field containing a challenge applicable to the requested resource. See Basic access authentication and Digest access authentication.

Reference HTTP status codes: http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
Basic access authentication: http://en.wikipedia.org/wiki/Basic_access_authentication

woodykiddy
  • 6,074
  • 16
  • 59
  • 100
  • I did try with following code but no luck. header: { "Authorization": "Basic username:password" } – HardCode Oct 01 '12 at 15:21
  • I did but gives me js error! EX: "Authorization": "Basic " + Base64.encode("username:password") – HardCode Oct 01 '12 at 15:34
  • maybe try another approach, create following function setHeader(xhr) { xhr.setRequestHeader('authorization', 'Basic Base64.encode('username:password')'); } and add setHeader to beforeSend – woodykiddy Oct 01 '12 at 15:40
  • You might also want to take a look at this post and that perhaps would give you something - http://stackoverflow.com/a/9570852/786796 – woodykiddy Oct 01 '12 at 15:50
  • I guess the issue is, I'm running on the localhost. I will try to run on the hosted server. – HardCode Oct 01 '12 at 16:08
  • Thank you for your help. I guess I found the answer in here: http://stackoverflow.com/questions/9327218/access-control-allow-origin-not-allowed-by – HardCode Oct 01 '12 at 16:17
  • No worries. Glad that you found your answer yourself. – woodykiddy Oct 02 '12 at 02:38