Search code examples
c#authenticationeventsblazorblazor-webassembly

How to run code on login in Blazor WebAssembly?


I'm trying to run a function every time a user logs in. What I tried is to obtain an instance of AuthenticationStateProvider from the DI container and subscribe to its AuthenticationStateChanged event. The problem is that my login pages are implemented using server-rendered Razor Pages so the page refreshes after a successful login. This means that my code subscribes to the event after it is fired, so my handler function never runs. Can anybody see a way to run custom code upon successful login?


Solution

  • If you're using the token based authentication (API), you can attach an event handler to the RemoteAuthenticatorView component located in the Authentication component. I think it has an event that is invoked immediately after a user log in. As far as I recall, I've once answered a similar question that way, and then changed my answer to something more useful. Try this solution, and let us know if it's OK.