【问题标题】:Authenticating remote access to Sharepoint 2016 on-premises via REST API通过 REST API 验证对本地 Sharepoint 2016 的远程访问
【发布时间】:2018-03-08 11:23:34
【问题描述】:

我正在尝试将远程 Web 应用程序连接到本地 SharePoint 2016 解决方案。我坚持进行身份验证,因此我可以从 SharePoint 访问列表。它只能是只读的。

这是我的电话(来自 Postman):

var settings = {
"async": true,
"method": "POST",
"url": "http://mycompanywebsite/sites/home/_api/Web/Lists('guid'55a15262-34d5-4b5c-aec9-27fdc1c02589')",

"headers": {
"X-RequestDigest": "Unique ID Here",
"Content-Type":'application/json'
 }
}

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

任何尝试都遇到“401 Authorized”错误。使用 Fiddler 的“自动验证”功能,我能够得到不同的结果:

尝试 A。) 通过添加以下自定义标头来更新服务器上的 web.config 文件:

<add name="Access-Control-Allow-Origin" value="http://localhost:1234" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
<add name="Access-Control-Allow-Methods" value="GET,POST,PUT,DELETE,OPTIONS" />
<add name="Access-Control-Allow-Credentials" value="true"/>

这会导致:

Response to preflight request doesn't pass access control check: The 'Access-Control-Allow-Origin' header contains multiple values '* 

尝试 B。) 通过启用“Access-Control-Allow_Origin”来更新服务器上的 web.config 文件,如下所示:

<add name="Access-Control-Allow-Origin" value="*" />

这会导致:

Response to preflight request doesn't pass access control check: The 'Access-Control-Allow-Origin' header contains multiple values '* 

尝试 C.) 我在上面的 AJAX 调用中添加了 "crossDomain": true 属性。

"crossDomain": true,

这会导致:

Response to preflight request doesn't pass access control check: The 'Access-Control-Allow-Origin' header contains multiple values '* 

我不确定我在哪里出错了。我能够毫无问题地访问内部 SharePoint,访问我尝试对其进行身份验证的 SharePoint URL,但什么也没有。

【问题讨论】:

标签: jquery ajax sharepoint cors fiddler


【解决方案1】:

此问题通常与后端服务器有关,但如果您无权访问服务器,那么您有两种选择

第一个选项使用这个 chrome 扩展:Allow-Control-Allow-Origin 但不幸的是,这个扩展在其他浏览器中不可用,所以你需要使用

第二个选项,使用在线 CORS 代理,如

https://cors-anywhere.herokuapp.com/http://example.com

http://cors-proxy.htmldriven.com/?url=http://www.htmldriven.com/sample.json

CORS 代理是一项免费服务,适用于需要绕过与向第三方服务执行标准 AJAX 请求相关的同源策略的开发人员。

这是一个完整的 Ajax 请求示例

  $.ajax({
            url: 'https://cors-anywhere.herokuapp.com/http://example.com',
            type: 'POST',
            beforeSend: function (xhr) {
                xhr.setRequestHeader('Authorization', 'bearer t-7614f875-8423-4f20-a674-d7cf3096290e');
                xhr.setRequestHeader("Access-Control-Allow-Origin", "*");
                xhr.setRequestHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
                xhr.setRequestHeader("Access-Control-Allow-Methods", "GET, POST, PATCH, PUT, DELETE, OPTIONS");
                xhr.setRequestHeader("Access-Control-Allow-Credentials", true);
            },
            headers: {
                "Access-Control-Allow-Origin" : "*",
                'Content-Type': 'application/json',
                'Accept': 'application/json',
           }
            xhrFields: {
                    'withCredentials': true
            },
            dataType: 'json',
            crossDomain: true
            data: {},
            success: function () { },
            error: function () { },
            });

        });

    });

我添加了withCredentials(),它使您的浏览器在您的 XHR 请求中包含 cookie 和身份验证标头。如果您的服务依赖于任何 cookie(包括会话 cookie),则它仅适用于此选项集。

【讨论】:

  • withCredentials 不适用于 Sharepoint 2016 rest api,并且 iis 中的 url 重写似乎也不起作用,似乎唯一的解决方案是调用一个调用 sharepoint 2016 api 的 web api
  • 你试过用chrome扩展:Allow-Control-Allow-Origin吗?
  • 是的,我认为 sharepoint 中的安全性已经发生了变化,因为它适用于 2013,但确实读过安全模型已经随着 Web 服务而变化。它在 sharepoint 内部运行良好,因此最好的解决方案是创建一个自定义 web api 并从服务器端调用 sharepoint 。谢谢
猜你喜欢
  • 1970-01-01
  • 2019-10-23
  • 1970-01-01
  • 2015-07-22
  • 1970-01-01
  • 1970-01-01
  • 2017-08-06
  • 2021-11-10
  • 1970-01-01
相关资源
最近更新 更多