Adventures with Blazor: Login and Logout Events


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!

15 Replies to “Adventures with Blazor: Login and Logout Events”

    1. The action is provided by the Blazor App, the only thing you should have to worry about is responding to the events.

  1. Hi Matt. I used your example in my Blazor Client App but OnLogInSucceeded is called only once after first login attempt. Is it a right behaviour or am I doing something wrong?

  2. Hi thanks. One problem (: When I hook up any events in my login flow It looks like I authentication 3 times more and I also got extra redirections

    1. That’s interesting, I have not encountered that problem. Does it eventually login? Wonder if there is an issue with the token being issued?

        1. Interesting … I am NOW (first time I wrote this I had not) seeing the same behavior in a new Blazor App I started.

      1. Thanks for opening the issue Matt. I am also seeing the extra calls on the authorization.razor login successful action.

Leave a Reply

Your email address will not be published.