来自SharePoint Online的具有AD身份验证的Azure函数的Java语言调用

时间:2018-11-15 20:24:29

标签: azure sharepoint azure-functions sharepoint-online

我对天蓝色函数的调用有一个大问题。身份验证为OFF时,我的azure功能可以完美运行。在SharePoint Online中,我有以下JS代码:

var serviceURL = "https://xxxxxxxxx.azurewebsites.net/api/Function1?name=test";

$.ajax({
    url: serviceURL,
    type: "POST",
    dataType: 'JSON',
    crossDomain: true,
    success: function (data) {
        alert("Success: " + data);
    },
    error: function (ex) {
        alert("Failure getting user token");
    }
});

好的,这可以正常工作。但是,当我在mu Azure Function上设置Active Directory身份验证并将代码更改为此时:

    var serviceURL = "https://xxxxxxxxx.azurewebsites.net/api/Function1?name=test";

$.ajax({
    url: serviceURL,
    type: "POST",
    dataType: 'JSON',
    crossDomain: true,
    headers: {
        "Content-Type": "'application/json;odata=verbose'",
        "Accept": "application/json;odata=verbose",
        "crossDomain": "true",
        "credentials": "include",
        "Access-Control-Allow-credentials": "true"
    },
    xhrFields: {
        withCredentials: true
    },
    success: function (data) {
        alert("Success: " + data);
    },
    error: function (ex) {
        alert("Failure getting user token");
    }
});

我收到此错误

  

SEC7122:[CORS]源'https://xxxxxxxx.sharepoint.com'使用凭据模式'include'发出了请求,但未找到跨行的值为'true'的Access-Control-Allow-credentials响应标头原始资源位于“ {https://xxxxxxxx.azurewebsites.net/api/Function1?name=test”。

我的azure函数看起来像这样

[FunctionName("Function1")]
    public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log)
    {
        log.Info("C# HTTP trigger function processed a request.");

        // parse query parameter
        string name = req.GetQueryNameValuePairs()
            .FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0)
            .Value;

        if (name == null)
        {
            // Get request body
            dynamic data = await req.Content.ReadAsAsync<object>();
            name = data?.name;
        }

        return name == null
            ? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string or in the request body")
            : req.CreateResponse(HttpStatusCode.OK, "Hello " + ClaimsPrincipal.Current.Identity.Name);
    }

我已允许在CORS设置中设置我的共享点域,所以一切正常...我已经测试了所有内容,但没有任何帮助解决该问题...同样的问题...在我的azure函数中,我已经测试过设置express或用于AD身份验证的高级管理模式,但没有帮助。...谢谢您的帮助。

0 个答案:

没有答案