Skip to content

[Tutorial] Advice sought: Handling expired tokens during an javascript fetch request #264

Open
@jadjare

Description

@jadjare

Please provide us with the following information:

This issue is for a: (mark with an x)

- [x] request for help
- [ ] bug report -> please search issues before submitting
- [ ] feature request
- [ ] documentation issue or request
- [ ] regression (a behavior that used to work and stopped in a new release)

The issue was found for the following scenario:

Please add an 'x' for the scenario(s) where you found an issue

  1. Other (please describe)
    1. Handling the an Expired Access Token during an AJAX based call
      1. Microsoft.Identity.Web
      2. Using TokenAcquisition to retrieve a token to be used for calling the graph method checkMemberGroups
      3. Making a AJAX based call to an ActionMethod requiring Azure AD authorization

Do you have any advise on how to handle the following situation?

When using the fetch javascript method to call an ActionMethod that requires an AccessToken, if the access token has expired, then a 302 redirect response is received. The redirect to login.microsoftonline.com then appears in the Network traffic with a 200 OK status response. However, the final redirection back to the client app (localhost) fails.

Chrome reports these errors/warnings duing this process

Access to fetch at 'https://login.microsoftonline.com/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx/oauth2/v2.0/authorize?client_id=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx&redirect_uri=https%3A%2F%2Flocalhost%3A44306%2Fsignin-oidc&response_type=code%20id_token&scope=openid%20profile%20offline_access%20User.Read&response_mode=form_post&nonce=12345&state=6789&x-client-SKU=ID_NETSTANDARD2_0&x-client-ver=5.3.0.0' (redirected from 'https://localhost:44306/Home/CheckMembership?groups=3c479d63-xxxx-xxxx-xxxx-9ce22ce90d13') from origin 'https://localhost:44306' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
Cross-Origin Read Blocking (CORB) blocked cross-origin response https://login.microsoftonline.com/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx/oauth2/v2.0/authorize?client_id=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx&redirect_uri=https%3A%2F%2Flocalhost%3A44306%2Fsignin-oidc&response_type=code%20id_token&scope=openid%20profile%20offline_access%20User.Read&response_mode=form_post&nonce=12345&state=6789&x-client-SKU=ID_NETSTANDARD2_0&x-client-ver=5.3.0.0 with MIME type text/html. See https://www.chromestatus.com/feature/5629709824032768 for more details.

The error received in the fetch request contains no details other than "TypeError: Failed to fetch".

Here's the javascript for the fetch

            let response = await fetch(`/Home/CheckMembership?groups=${checkGroupIds}`)
                .catch(err => console.log({ method: "CheckMembership", error: err }));

I'm guessing that a MsalUIRedirectionException is being thrown under the hood and this needs to be appropriately handled in the fetch method, however, I cannot see how to do it. I have checked various sample projects but none of them make requests via an AJAX style call.

Please let me know if you need more information.

Metadata

Metadata

Assignees

Labels

cssThis issue is also a support case or similar to a support case in progressenhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions