微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – 401尝试实现CORS for SharePoint时

我想从位于domainB.contoso.com上的Web应用程序访问位于domainA.contoso.com上的listdata.svc(一个sharepoint服务) – 身份验证似乎是个问题.

当尝试通过JQuery Ajax调用访问ListData.svc时,启用CORS,服务器返回401.如果我从SharePoint内部执行的.htm页面运行相同的查询,则调用正常,因为域名是相同.

SharePoint正在使用NTLM关闭匿名身份验证 – 我认为401是Windows凭据未传递到SharePoint服务器的结果 – 但我不知道如何将这些凭据正确添加到标头.我已经设置了xhrFields:{withCredentials:true},但这似乎无法解决身份验证问题.

为了启用CORS,我在IIS中的SharePoint上设置了以下HTTP响应标头:

> Access-Control-Allow-Credentials:true
> Access-Control-Allow-Headers:Origin,Content-Type,Accept
> Access-Control-Allow-Origin:*
>访问控制请求方法:POST,GET,HEAD,OPTIONS

在我的Web应用程序的IIS中启用了Windows身份验证,我没有在IIS中设置“OPTIONsverbHandler”HTTP处理程序.把它翻到阅读似乎没有什么区别.

JQuery Ajax调用(来自subdomainB.contoso.com上的应用程序):

$.ajax({
        type: "GET",contentType: "application/json; charset=utf-8",url: listUrl,xhrFields: { withCredentials: true },crossDomain:true,processData: false,async: true,dataType: "json",converters: {
            // WCF Data Service .NET 3.5 incorrectly escapes singles quotes,which is clearly
            // not required (and incorrect) in JSON specs.
            // http://bugs.jquery.com/ticket/8320?cversion=0&cnum_hist=1
            "text json": function (textValue) {
                return jQuery.parseJSON(textValue.replace(/(^|[^\\])\\'/g,"$1'"));
            }
        },success: function (data,status,xhr) {
            //successFunc(data.d.results);
            alert("working!");
        },error: function (xhr,error) {
            alert("failure!");
        }
    });

HTTP标头和401响应:

Key Value
Request OPTIONS /_vti_bin/ListData.svc/Contacts HTTP/1.1
Accept  */*
Origin  http://domainB.contoso.com
Access-Control-Request-Method   GET
Access-Control-Request-Headers  content-type,accept
Accept-Encoding gzip,deflate
User-Agent  Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)
Host    domainA.contoso.com
Content-Length  0
DNT 1
Connection  Keep-Alive
Cache-Control   no-cache

Key Value
Response    HTTP/1.1 401 Unauthorized
Server  Microsoft-IIS/7.5
SPRequestGuid   1e33061c-f555-451b-9d69-0d83eff5f5ea
WWW-Authenticate    NTLM
X-Powered-By    ASP.NET
MicrosoftSharePointTeamServices 14.0.0.4762
Access-Control-Allow-Headers    Origin,Accept
Access-Control-Allow-Origin *
Access-Control-Request-Methods  POST,OPTIONS
Access-Control-Allow-Credentials    true
Date    Wed,15 May 2013 15:04:51 GMT
Content-Length  0

解决方法

迟到的响应,但我发现另一个线程 here,它有一个简单的IIS解决方案,并为我工作.

基本上CORS标准规定了预检请求不应该发送任何认证信息,因此401.在该线程中有一个例子,限制对OPTIONS动词的匿名请求,允许对预检(OPTIONS动词)请求做出200响应,但仍需要对其他人的身份验证.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐