Adventures with Blazor: Login and Logout Events

blazor

Ever wanted to do something after a user logs in or logs out of your Blazor App?

Surprisingly, it fairly simple to do.

In VS Code I created a Blazor WebAssembly project that uses Azure ADB2C for authentication.

dotnet new blazorwasm -au IndividualB2C --aad-b2c-instance "{AAD B2C INSTANCE}" --client-id "{CLIENT ID}" --domain "{TENANT DOMAIN}" -ssp "{SIGN UP OR SIGN IN POLICY}" -o "{FOLDER_NAME}"

Once scaffolded, I navigated to the Authenticate.razor page in the Pages folder.

The RemoteAuthenticatorView component supports two events that you can wire up a callback to, OnLogInSucceeded and OnLogOutSucceeded.

I just created two methods OnLogInSucceeded and OnLogOutSucceeded and wired them up to the appropriate events in the RemoteAuthenticatorView.

Now when the user logs in I can get additional profile data from my database and/or add a new user to my database, or if the user logs out I can clear the cache.

That’s it!

Pretty easy and pretty powerful!

Leave a Reply

Your email address will not be published.