How to gain access to web app(that serves also as api) from SPA application?

Max R 0 Reputation points
2023-12-11T22:07:34.4933333+00:00

Hi,
I have a question regarding the solution of using one web app and other spa which needs to call web app to get some data. How to manage the authentication between them? Do you have any suggestions?

I know that for web app I could use sessions and login the user by setting confidential client application. However I also need to be able to silently sign on to the app within the iframe. So the requirements are:

  1. Be able to request data from web app - on the SPA pages.
  2. Be able to authorize in an iframe in a web app (no redirection).
  3. Be able to authorize in a web app (outside of iframe).

I tried few ideas like using msal for silent sign on - but then there are few more questions on how to access to graph api. Usually spa has to request the token for the resource not to a web app. This is confusing. Could someone clarify it to me? What's the best option to set it up?

Developer technologies | ASP.NET | ASP.NET Core
Microsoft Security | Microsoft Graph
Developer technologies | ASP.NET | Other
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 81,711 Reputation points Volunteer Moderator
    2023-12-12T22:48:51.0433333+00:00

    if I understand, you have a webapi, and a spa application (if spa hosted by seperate website, the webapi need to enable CORS)

    typically you would use JWT tokens for the SPA to authenticate with the server. msal will use oauth to get the token, but azure ad (like many oauth providers) does not support authenication via an iframe.

    to call the graphapi using SPA token, the app must be register with azure ad and configured to have graphapi access. typically the user will need to authorize the access during login (must admin my prevent this).

    so the site hosting the spa, is the app registered site. when authentication is required, the spa redirects to azure login. after login azure redirects back to the hosting site, the hosting reloads the spa passing the token on the url, which msal will parse and make available via get token silently. as typically a refresh token is returned, the msal library can use the refresh to get a new access token. if the refresh token is expired, the the msal library will redirect to login server.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.