MSAL Angular auth code flow

Chekuri, Suneetha 1 Reputation point
2022-02-16T17:46:30.827+00:00

We would like to get authorization code on our angular app and pass the code node server and redeem the token on node server. Is it possible to get authorization code using msal-angular? Any recommendations on how this can be achieved?

Microsoft Security Microsoft Entra Microsoft Entra ID
{count} votes

1 answer

Sort by: Most helpful
  1. Shweta Mathur 30,296 Reputation points Microsoft Employee Moderator
    2022-02-17T06:25:21.91+00:00

    Hi @Chekuri, Suneetha ,

    Thanks for reaching out.

    I understand that you are trying to get access token using Authorization code flow from an angular web Application which is now supported by MSAL Angular v2 library.

    The @azure/msal-angular enable authentication in Angular Web Applications without backend servers. This version of the library uses the OAuth 2.0 Authorization Code Flow with PKCE.

    First you need to register the angular application MSAL.js 2.0 with auth code flow.
    Select the application registered in Azure portal.
    Under manage->Select Authentication-> Add platform.
    Under Web Application, select the Single-page application.

    175188-pic1.png

    Do NOT select either checkbox under Implicit grant and hybrid flows.

    By configuring your redirect URI using the Single-page application tile in the Add a platform pane, your application registration is configured to support the authorization code flow with PKCE and CORS.
    175274-pic2.png

    Configure the required application registered values to the angular application config.

    Also you need to register the node server API in Azure Portal.
    In the app's registration screen, select the Expose an API blade to the left to open the page where you can declare the parameters to expose this app as an Api for which client applications can obtain access token for and add those permissions in web application.

    Install angular package on NPM.
    npm install @azure/msal-browser @azure/msal-angular@latest

    Below is the sample you can refer to get the token using authorization code flow to call the Microsoft Graph API. In the same way you can call custom protected API registered in Azure AD.

    https://learn.microsoft.com/en-us/azure/active-directory/develop/tutorial-v2-angular-auth-code

    Hope this will help.

    Thanks,
    Shweta

    --------------------------------------------------------------------------

    Please remember to "Accept Answer" if answer helped you.


Your answer

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