This is a Chrome Extension that can be used as example template demostrating how to consume an external WebAPI secured with Azure AD B2C. The example illustrates the the Todo List example as a SPA application embedded in a Chrome extension with MSAL.js login implementation, by adapting the original example provided by Microsoft at https://github.com/Azure-Samples/active-directory-b2c-javascript-msal-singlepageapp/tree/master/JavaScriptSPA
Follow these steps to setup the Chrome Extension:
- Follow the instructions described at https://github.com/intelequia/dnn.azureadb2cprovider/tree/master/samples/active-directory-b2c-dotnet-webapp-and-webapi to setup the TodoList Service (WebAPI)
- Download the Chrome extension code available on the "/src" folder
- Open the Chrome extension settings chrome://extensions
- Enable the "developer mode" by changing the slider on the top-right area of the screen
- Click on the button "Load uncompressed" extension and select the folder
- Copy the Chrome extension Id that appears on the extension details to a notepad
- Go to the Azure Portal and select the B2C tenant that was used to setup the WebAPI on step #1 on https://portal.azure.com/#blade/Microsoft_AAD_B2CAdmin/TenantManagementMenuBlade/manageApplications
- Add a new Application Registration:
- Set a new name for the application (i.e. "Todo List Chrome Extension")
- Enable the Include WebApp/WebAPI checkbox
- Enable the Allow implicit flow checkbox
- Set the reply URL to "https://localhost". We will be changing this value on steps below
- Leave the optioanl APP ID URL empty
- Don't enable the Native Client and click on the Create button
- Copy the Application ID to a notepad, you will be using this value later
- On the API Access section of the application click on "Add" and select the WebAPI scoes read and write you created on step #1
- Now we need to change the reply URL https://localhost for chrome-extension://{ChromeExtensionId}/todolist.html, but you can't do this through the application management UI, you need to directly edit the manifest. To change this:
- Go to the Azure AD App registrations section at https://portal.azure.com/#blade/Microsoft_AAD_IAM/ActiveDirectoryMenuBlade/RegisteredApps (using the regular Azure AD management, not the B2C) and locate the application you created on step #8
- On the Manifest section of the application, edit the manifest chaning the "replyUrlsWithType" section, replacing "https://localhost" for "chrome-extension://{ChromeExtensionId}/todolist.html", where {ChromeExtensionId} is the value you copied on step #6, and click on Save
- Edit the files apiConfig.js to specify the scope names and endpoint of the WebAPI you created on step #1
- Edit the file policies.js to specify the policy names and authorities
- Edit background.js to indicate on which websites you want to enable the extension (or change the logic for what's better for you)
- Finally edit the file authConfig.js and specify the Application ID you copied on step #8.7
- Reload the extension on Chrome