Javascript call of Azure Function with AD authentification from SharePoint Online










1















I have one big problem with call of azure function. My azure function work perfectly when authentication is OFF. In SharePoint online I have this JS code:



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");

);


Ok so this is working. But When I setup Active Directory Authentication on mu Azure Function and change my code to this:



 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");

);


I get this error




SEC7122: [CORS] The origin 'https://xxxxxxxx.sharepoint.com' made a request using credentials mode 'include' and did not find an Access-Control-Allow-credentials response header with a value of 'true' for cross-origin resource at 'https://xxxxxxxx.azurewebsites.net/api/Function1?name=test'.




My azure function look like this



[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);



I have allowed in CORS settings my sharepoint domain so there is everything ok... I have tested everything but nothing helps me with this problem...Same problem...In my azure function I have tested to setup express or advanced management mode for AD authentification but nothing helsp....Thank you for your help.










share|improve this question

















  • 1





    Likely related to github.com/Azure/azure-functions-host/issues/620, see this issue too: stackoverflow.com/questions/53321897/…

    – Marie Hoeger
    Nov 16 '18 at 0:30











  • Hello. Thank you for your help. The problem is if I remove my domain from cors list the problem is little different: SEC7120: [CORS] The origin 'xxxxxxxxx.sharepoint.com' did not find 'xxxxxxxxxx.sharepoint.com' in the Access-Control-Allow-Origin response header for cross-origin resource at 'xxxxxxxxxxx.azurewebsites.net/api/Function1?name=test

    – user1223484
    Nov 16 '18 at 13:20











  • If I call my function directly from web browser then I must login and after that everything works...But from javascript there still same error.

    – user1223484
    Nov 16 '18 at 22:10











  • if you remove all cors from the portal, you have to send the header in the response manually

    – Thomas
    Nov 18 '18 at 4:35















1















I have one big problem with call of azure function. My azure function work perfectly when authentication is OFF. In SharePoint online I have this JS code:



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");

);


Ok so this is working. But When I setup Active Directory Authentication on mu Azure Function and change my code to this:



 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");

);


I get this error




SEC7122: [CORS] The origin 'https://xxxxxxxx.sharepoint.com' made a request using credentials mode 'include' and did not find an Access-Control-Allow-credentials response header with a value of 'true' for cross-origin resource at 'https://xxxxxxxx.azurewebsites.net/api/Function1?name=test'.




My azure function look like this



[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);



I have allowed in CORS settings my sharepoint domain so there is everything ok... I have tested everything but nothing helps me with this problem...Same problem...In my azure function I have tested to setup express or advanced management mode for AD authentification but nothing helsp....Thank you for your help.










share|improve this question

















  • 1





    Likely related to github.com/Azure/azure-functions-host/issues/620, see this issue too: stackoverflow.com/questions/53321897/…

    – Marie Hoeger
    Nov 16 '18 at 0:30











  • Hello. Thank you for your help. The problem is if I remove my domain from cors list the problem is little different: SEC7120: [CORS] The origin 'xxxxxxxxx.sharepoint.com' did not find 'xxxxxxxxxx.sharepoint.com' in the Access-Control-Allow-Origin response header for cross-origin resource at 'xxxxxxxxxxx.azurewebsites.net/api/Function1?name=test

    – user1223484
    Nov 16 '18 at 13:20











  • If I call my function directly from web browser then I must login and after that everything works...But from javascript there still same error.

    – user1223484
    Nov 16 '18 at 22:10











  • if you remove all cors from the portal, you have to send the header in the response manually

    – Thomas
    Nov 18 '18 at 4:35













1












1








1








I have one big problem with call of azure function. My azure function work perfectly when authentication is OFF. In SharePoint online I have this JS code:



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");

);


Ok so this is working. But When I setup Active Directory Authentication on mu Azure Function and change my code to this:



 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");

);


I get this error




SEC7122: [CORS] The origin 'https://xxxxxxxx.sharepoint.com' made a request using credentials mode 'include' and did not find an Access-Control-Allow-credentials response header with a value of 'true' for cross-origin resource at 'https://xxxxxxxx.azurewebsites.net/api/Function1?name=test'.




My azure function look like this



[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);



I have allowed in CORS settings my sharepoint domain so there is everything ok... I have tested everything but nothing helps me with this problem...Same problem...In my azure function I have tested to setup express or advanced management mode for AD authentification but nothing helsp....Thank you for your help.










share|improve this question














I have one big problem with call of azure function. My azure function work perfectly when authentication is OFF. In SharePoint online I have this JS code:



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");

);


Ok so this is working. But When I setup Active Directory Authentication on mu Azure Function and change my code to this:



 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");

);


I get this error




SEC7122: [CORS] The origin 'https://xxxxxxxx.sharepoint.com' made a request using credentials mode 'include' and did not find an Access-Control-Allow-credentials response header with a value of 'true' for cross-origin resource at 'https://xxxxxxxx.azurewebsites.net/api/Function1?name=test'.




My azure function look like this



[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);



I have allowed in CORS settings my sharepoint domain so there is everything ok... I have tested everything but nothing helps me with this problem...Same problem...In my azure function I have tested to setup express or advanced management mode for AD authentification but nothing helsp....Thank you for your help.







azure sharepoint azure-functions sharepoint-online






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 20:24









user1223484user1223484

90111




90111







  • 1





    Likely related to github.com/Azure/azure-functions-host/issues/620, see this issue too: stackoverflow.com/questions/53321897/…

    – Marie Hoeger
    Nov 16 '18 at 0:30











  • Hello. Thank you for your help. The problem is if I remove my domain from cors list the problem is little different: SEC7120: [CORS] The origin 'xxxxxxxxx.sharepoint.com' did not find 'xxxxxxxxxx.sharepoint.com' in the Access-Control-Allow-Origin response header for cross-origin resource at 'xxxxxxxxxxx.azurewebsites.net/api/Function1?name=test

    – user1223484
    Nov 16 '18 at 13:20











  • If I call my function directly from web browser then I must login and after that everything works...But from javascript there still same error.

    – user1223484
    Nov 16 '18 at 22:10











  • if you remove all cors from the portal, you have to send the header in the response manually

    – Thomas
    Nov 18 '18 at 4:35












  • 1





    Likely related to github.com/Azure/azure-functions-host/issues/620, see this issue too: stackoverflow.com/questions/53321897/…

    – Marie Hoeger
    Nov 16 '18 at 0:30











  • Hello. Thank you for your help. The problem is if I remove my domain from cors list the problem is little different: SEC7120: [CORS] The origin 'xxxxxxxxx.sharepoint.com' did not find 'xxxxxxxxxx.sharepoint.com' in the Access-Control-Allow-Origin response header for cross-origin resource at 'xxxxxxxxxxx.azurewebsites.net/api/Function1?name=test

    – user1223484
    Nov 16 '18 at 13:20











  • If I call my function directly from web browser then I must login and after that everything works...But from javascript there still same error.

    – user1223484
    Nov 16 '18 at 22:10











  • if you remove all cors from the portal, you have to send the header in the response manually

    – Thomas
    Nov 18 '18 at 4:35







1




1





Likely related to github.com/Azure/azure-functions-host/issues/620, see this issue too: stackoverflow.com/questions/53321897/…

– Marie Hoeger
Nov 16 '18 at 0:30





Likely related to github.com/Azure/azure-functions-host/issues/620, see this issue too: stackoverflow.com/questions/53321897/…

– Marie Hoeger
Nov 16 '18 at 0:30













Hello. Thank you for your help. The problem is if I remove my domain from cors list the problem is little different: SEC7120: [CORS] The origin 'xxxxxxxxx.sharepoint.com' did not find 'xxxxxxxxxx.sharepoint.com' in the Access-Control-Allow-Origin response header for cross-origin resource at 'xxxxxxxxxxx.azurewebsites.net/api/Function1?name=test

– user1223484
Nov 16 '18 at 13:20





Hello. Thank you for your help. The problem is if I remove my domain from cors list the problem is little different: SEC7120: [CORS] The origin 'xxxxxxxxx.sharepoint.com' did not find 'xxxxxxxxxx.sharepoint.com' in the Access-Control-Allow-Origin response header for cross-origin resource at 'xxxxxxxxxxx.azurewebsites.net/api/Function1?name=test

– user1223484
Nov 16 '18 at 13:20













If I call my function directly from web browser then I must login and after that everything works...But from javascript there still same error.

– user1223484
Nov 16 '18 at 22:10





If I call my function directly from web browser then I must login and after that everything works...But from javascript there still same error.

– user1223484
Nov 16 '18 at 22:10













if you remove all cors from the portal, you have to send the header in the response manually

– Thomas
Nov 18 '18 at 4:35





if you remove all cors from the portal, you have to send the header in the response manually

– Thomas
Nov 18 '18 at 4:35












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53327379%2fjavascript-call-of-azure-function-with-ad-authentification-from-sharepoint-onlin%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53327379%2fjavascript-call-of-azure-function-with-ad-authentification-from-sharepoint-onlin%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

政党

天津地下鉄3号線