question

PushpanjayUmrao-3126 avatar image
0 Votes"
PushpanjayUmrao-3126 asked AmosWu-MSFT answered

CORS issue while accessing Azure function from SPFx Webpart

I have created an Azure function that uses CSOM and Graph api to get site owner details. The Azure function is then triggered from a SPFx webpart. I was earlier using admin creds directly in the code for CSOM calls and hardcoded client id and client secret for Graph api. I had also added my tenant url in the 'Allowed Origin' it was working perfectly until now. However due to company policy i had to use 3rd party certificate for csom connection and instead of directly adding Graph api details i used key vault to generate secret and then use it in code. Now the issue started coming. When i run the azure function directly by running the url in browser it now redirects me to Microsoft login page and when enter my creds it shows me the response. Now when i run the url from my spfx webpart it starts giving me cors error as shown in the attached screenshot.
43226-corsissue.png

My understanding is that the azure function is redirecting to Microsoft Login page and that page is causing this error.

I also tried adding https://login.windows.net and https://login.microsoftonline.com in "Allow Origin" of CORS setting of the Azure function. Any idea how can i resolve this issue


office-sharepoint-onlineazure-functions
corsissue.png (40.7 KiB)
· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hello @PushpanjayUmrao-3126 Did you add the Office 365 tenant domain URL and SharePoint local workbench URL in the " Allowed origin" list?
You may refer to this.
Also, let me know if you could share the screenshot of that list offline.



0 Votes 0 ·

Hi Jaya,

Yes i have added the tenant domain URL and local workbench also. However i am testing it in site workbench whose tenant url i have already added in " Allowed origin" list. Yes I can send you the screenshot offline. please tell me how. Also as i mentioned earlier before using 3rd party certificate thumbprint and key vault for csom authentication, it was running fine even from workbench using my spfx webpart. Earlier it didnt redirected to microsoft login page and hence i think no issue came.

0 Votes 0 ·

Have you uploaded the .cer file to Azure AD app?

0 Votes 0 ·

yes I did. And I am using the the thumbprint of the same for authntication

0 Votes 0 ·

Few update on this. there were few mistakes in my config which i corrected. I found two issues. 1) cors issue is coming even when i am using simple context.credentials for authentication. this works fine in browser but wen i use it through my spfx webpart it gives error. 2) With certificate it plainly shows internal error even when run in browser. As mentioned earlier it was working fine some time back when i authenticate with context.credentials. I think the issue came when i added SharePointPnPCoreOnline nuget package. because of this i had to downgrade my graph api to version 1.21 as there was a conflict for a dll. i am attaching my code and my assembly screenshot.
44045-code.png


0 Votes 0 ·
code.png (41.9 KiB)

1 Answer

AmosWu-MSFT avatar image
0 Votes"
AmosWu-MSFT answered
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.