Access to fetch at 'https://login.microsoftonline.com/<tenantid>/oauth2/v2.0/authorize' (redirected from 'https://localhost:44368/api/values') from origin 'sharepoint tenant domain' has been blocked by CORS policy
Category
- [x] Bug
Describe the bug
When I try to access a Azure AD secure API hosted on local IIS Express in my spfx webpart using the aadhttpclient, I get the following error
Access to fetch at 'https://login.microsoftonline.com/
Steps to reproduce
-
Register a azure ad app. Use the quickstart to download the sample from the registered app in the azure portal.
-
This downloads the sample code from https://github.com/AzureADQuickStarts/AppModelv2-WebApp-OpenIDConnect-DotNet with the required config values like client id, tenant id and redirect URI (localhost:44368).
-
Update the same redirect URI localhost:44368 in the azure ad app registration as well. Add a scope user_impersonation and under implicit grant id tokens is enabled.
-
Make the required changes to the code downloaded like adding a sample API controller, changes to global.asax to register webapiconfig, decorating the sample controller with Authorize attribute also add EnableCors attribute with origin as SharePoint top level site URL ( Tried with all origins (*) as well).
-
The api when accessed via browser works fine. asks the user to authenticate and returns response on successful authentication
-
Create a new SPFx webpart and update the webapipermissionrequest, add the aadhttpclient code to the webpart. Deploy the app. Approve the permissions from the api management page.
-
From the fiddler trace I see the following:
The API endpoint is called successfully with a bearer token. This results in a response "{"Message":"Authorization has been denied for this request."}". This also results in a 302 redirect to the authorization endpoint forn a ID token
https://login.microsoftonline.com/
This redirect results in the error mentioned above.
Environment details (development & target environment)
- Your Developer Environment: Windows 10
- Target Environment: SharePoint Online
- Browser(s): Chrome v85
- Tooling: VS Code | SPFx v1.11.0 | Visual Studio 2019]
Thank you for reporting this issue. We will be triaging your incoming issue as soon as possible.
@kunalkankariya said:
When I try to access a Azure AD secure API hosted on local IIS Express in my spfx webpart
Can you clarify what you're doing? You say "local IIS Express for SPFx web part". What do you mean by this?
The AzureADHttpClient API assumes you are already authenticated on the page, which means it only works in SharePoint Online.
@andrewconnell ,
The API is running on localhost (IIS express). The SPFx webpart is running on a hosted workbench.
The error occurs when the API endpoint is called by the SPFx webpart with a valid access token.
What's returning the error... Azure AD, your API, or SPO?
What's requesting the access token... your API or SPFx's AadHttpClient?
AadHttpClient is only going to give you an access token to call an endpoint, previously granted with the SPO API management page. You then use the AadHttpClient to call the endpoint and that HTTP request to your API will include the HTTP Authorization header that includes the access token.
That error in your OP is confusing as it looks like your locally hosted API is requesting the access token.
@andrewconnell ,
Yes this is exactly what happens.
The SPFx webpart (sharepoint online client extensibility) successfully gets an access token to access the API endpoint.
It first sends a pre-flight request to the api endpoint and gets a 200 OK response.
It then uses this access token (bearer) to call the API endpoint which is https://localhost:44368/api/values . The response to this a 302 redirect to
Location: https://login.microsoftonline.com/f4abc276***/oauth2/v2.0/authorize?client_id=
Which then results in the CORS error mentioned above.
Is something wrong with the way authentication is setup in the API? I suspect that's the issue.
It is similar to this issue #2537 - https://github.com/SharePoint/sp-dev-docs/issues/2537
@andrewconnell ,
I think my API is wrongly setup for authentication. There shouldn't be any redirect once the API is called with a valid access token.
Agreed... this isn't a SharePoint development issue... the problem is with how you've set up your API with Azure AD.
Yeah, realized this. I just setup authentication using Microsoft Identity Web instead of the Owin Middleware and it works like a charm. Closing this. Thank you!
Issues that have been closed & had no follow-up activity for at least 7 days are automatically locked. Please refer to our wiki for more details, including how to remediate this action if you feel this was done prematurely or in error: Issue List: Our approach to locked issues