My notes on setting up Blazor with AD B2C identity configured.
A list of the various material I used to build an end-to-end approach.
- Azure Active Directory B2C documentation
- Secure ASP.NET Core Blazor Server apps
- Get started with ASP.NET Core Blazor
- Tutorial - Enable your Web Apps to sign-in users and call APIs with the Microsoft identity platform for developers
- https://docs.microsoft.com/en-us/samples/azure-samples/active-directory-aspnetcore-webapp-openidconnect-v2/enable-webapp-signin/
- This last entry is a treasure map of Microsoft Identity.
- Just remember treasures are booby trapped.
This topic can be approached from the AAD B2C viewpoint, Blazor viewpoint, or developer viewpoint. No angle is any better or worse than the other, but you need all 3 views to put this together.
I like to start wide, and circle in to the details. Sort of like a bug circling the drain. So I'll take these in that order presented.
Documentation https://docs.microsoft.com/en-us/azure/active-directory-b2c/
https://docs.microsoft.com/en-us/azure/active-directory-b2c/tutorial-create-tenant
-
Just follow the steps. It is a good tutorial
-
For Azure AD B2C Create tenant I entered:
- Organization name = Blazor Server
- Initial domain name = bsomething
- Country or region = United States
-
Click on DEFAULT DIRECTORY for Switch Directory to display
- I had to wait like 5 minutes before clicking on Switch Directory would show the new tentant
- Add Azure AD B2C as a favorite (optional), this is tricky
https://docs.microsoft.com/en-us/azure/active-directory-b2c/tutorial-register-applications?tabs=applications Just follow the steps. Here are some notes that might help clarify things:
-
Step 6 – I believe is obsolete
-
Step 7
- Read through completely, lot of important info.
- Choose Web and https://jwt.ms for now.
-
Step 8 -- Click on Register, not Create
Note Lots of good documentation references on tenant page
- This doesn't go into a Blazor app, but maybe something else needs it.
https://docs.microsoft.com/en-us/azure/active-directory-b2c/tutorial-create-user-flows
-
User Flows are Policies that can be applied to many apps within a tenant, so they are selected and worked on separate from the application.
-
Just follow the instructions
-
Step 7.2 – email signup – Don't know how to do a User Name signup. Something TODO .
-
Step 7.4 - User attributes – I select everything, and then click on Show more... And select everything on the panel. During testing, it is a pain to have to enter all this in, but later, you get to see all the return claims in in your app.
-
NOTE: Claims in MS Identity are fantastic. It is a name-value pair table, so you don't have to change the db schema to add a new field, you just define a new claim type. Definitely worth the read to understand Claims.
- Follow the instructions
- Step 3.5 Will display the following in a browser
- This tenant is brand new, and therefore has no accounts in it. If you try to sign in, you get a "We can't seem to find your account" message. This is why the flow is SUSI, Sign Up, then Sign In.
- Use Sign up Now at bottom of the screen
- Step 5 – Verification email sometimes feels slow. Give it a couple of minutes. Fill out the sign up form.
- Step 6 – A whole lotta nothin' goin' on.
- Troubleshooting – Earlier, we used the Use App registrations (Preview). Turns out this should say (Premature) instead, because it didn't actually properly configure the app.
- Click on
- Click on
- Click on the application
- In the column on the right side, look for
- Under Implicit Grant
- Read what it says about only used in SPA apps. Someone smarter than me needs to investigate this.
- Now go ahead select Access Tokens and ID Tokens.
- Click on Save at the top of the screen.
- GOTO (ugh) Test the User Flow and repeat.
- Click on
- Voila! We got tokens and we got Claims. Idint (sic) that cool?
- Without Access and ID tokens check, nothing comes out. I don't know.
- Troubleshooting – Earlier, we used the Use App registrations (Preview). Turns out this should say (Premature) instead, because it didn't actually properly configure the app.
- Continue as before.
- Yep, continue.
-- At this point, switch over to Secure ASP.NET Core Blazor Server apps:
https://docs.microsoft.com/en-us/aspnet/core/security/blazor/server?view=aspnetcore-3.1&tabs=visual-studio
Before we get all crazy with additional identity providers, let make sure this one works!
The template supports Azure ADB2C out of the box when you choose
"Individual" authentication then choose the ADB2C sub-option.
https://github.com/dotnet/aspnetcore/issues/11787 Steve Sanderson, 2019-07-02
Lets prove him right.
- Configure your new Project
- Stop at Create new Blazor App
- Change the Authentication
- Switch to Individual User Accounts
- Change from Store user accounts in-app
- Domain Name
- From Azure AD B2C page (don't forget copy button at the end of each entry)* Domain = something.onmicrosoft.com
- Application ID
- CallBack Path
- Should be prepopulated with /signin-oidc Do not change
- Reply URI: https://locahost:44331/signing-oidc, Click on Copy
- Return to Azure screen and select Redirect URIs
- Sign-up or Sign-in Policy
- B2C_1_SUSI
- Reset Password Policy
- B2C_1_PasswordReset
- Edit Profile Policy
- B2C_1_ProfileEditing
- Click on OK.
- Click on Create.
- Open appsettings.json.
- Cut out the AzureAdB2C element.
- Right click on project, and select Manage User Secrets.
- Paste the AzureAdB2C inside.
- Save
- Now project ready to be save to repository
- NOTE: I left behind a non-functional example in the repo.