【问题标题】:Jquery+wcf. Problem with basic authenticationjQuery+wcf。基本身份验证问题
【发布时间】:2011-10-25 15:02:29
【问题描述】:

我的服务受到 IIS 中设置的基本身份验证的保护,我正在尝试使用 Jquery 从服务中获取数据。

已启用跨域调用。

我有下一个请求标头

Host http:\\service.com
User-Agent Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0
Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language en-gb,en;q=0.5
Accept-Encoding gzip, deflate
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection keep-alive
Origin null
Access-Control-Request-Me... GET
Access-Control-Request-He... authorization
Pragma no-cache
Cache-Control no-cache

回复

Content-Type text/html
Server Microsoft-IIS/7.5
WWW-Authenticate Basic realm="172.27.131.5"
X-Powered-By ASP.NET
Access-Control-Allow-Orig... *
Access-Control-Allow-Head... *
Date Fri, 12 Aug 2011 08:07:29 GMT
Content-Length 1293

代码

 $.ajax({
     headers : {
        "Authorization" : "Basic TVNF3TQtU1BGMjAx6C12bVxzbW4ydHBvaW50OlF3Z5J0eSEyM6Q1"
     },
     type: "GET",
     url: url,
     contentType: "application/json; charset=utf-8",
     dataType: "json",
     success: function(data) {
      alert('ok!');
      formatData(format_type,data);
     },
     error: function(jqXHR, textStatus, errorThrown) {
          alert(textStatus + ' / ' + errorThrown);
     }
  });

我也试过设置

 beforeSend : function(xhr) {
      xhr.setRequestHeader("Authorization", "Basic " + Base64.encode(username + ':' + password));
        },

但我有下一个错误:

Access-Control-Allow-Headers 不允许请求头域授权

我做错了什么?

【问题讨论】:

  • 尝试不带headers的ajax调用
  • 那么服务将如何进行身份验证?

标签: jquery ajax wcf authentication http-headers


【解决方案1】:
$.ajax({
    url: 'https://www.tejastank.com/moderator/v1/series?key='+key,
    data: myData,
    type: 'GET',
    crossDomain: true,
    dataType: 'jsonp',
    success: function() { alert("Success"); },
    error: function() { alert('Failed!'); },
    beforeSend: setHeader
});

解决Access-Control-Allow-Origin错误修改dataType参数为dataType:'jsonp'并添加crossDomain:true

【讨论】:

    【解决方案2】:

    尝试将以下代码添加到您的 WCF 项目中的 global.asax(仅在托管在 iis 中时有效):

    protected void Application_BeginRequest(object sender, EventArgs e)
    {
        EnableCrossDomainAjaxCall();
    }
    
    private void EnableCrossDomainAjaxCall()
    {
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
    
        if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
        {
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Authorization, Accept");
            HttpContext.Current.Response.End();
        }
    }
    

    Jquery 向 WCF 服务发出一个 OPTIONS 调用,以检查该调用是否被允许。您必须匹配确切的标题。您也可以在此处下载示例:http://sameproblemmorecode.blogspot.com/2011/10/creating-secure-restfull-wcf-service.html

    【讨论】:

      【解决方案3】:

      你需要在你的ajax调用中启用跨域

      $.ajax({
           headers : {
              "Authorization" : "Basic TVNF3TQtU1BGMjAx6C12bVxzbW4ydHBvaW50OlF3Z5J0eSEyM6Q1"
           },
           type: "GET",
           url: url,
           crossDomain:true, <--
           xhrFields: {
              withCredentials: true
           }, 
           contentType: "application/json; charset=utf-8",
           dataType: "json",
           success: function(data) {
            alert('ok!');
            formatData(format_type,data);
           },
           error: function(jqXHR, textStatus, errorThrown) {
                alert(textStatus + ' / ' + errorThrown);
           }
        });
      

      【讨论】:

        猜你喜欢
        • 2012-01-03
        • 2015-07-22
        • 2010-11-17
        • 1970-01-01
        • 1970-01-01
        • 2018-12-20
        • 2021-05-17
        • 2010-11-24
        相关资源
        最近更新 更多