Blazor Timer call async API task to update UI

You probably don’t want to Invoke() the GetValue(), that would be rather pointless. You can implement the timer like this: System.Threading.Timer timer; protected override void OnInitialized() { timer = new System.Threading.Timer(async _ => // async void { Time = await GetValue(); // we need StateHasChanged() because this is an async void handler // we need … Read more

Get Current User in a Blazor component

There are three possibilities for getting the user in a component (a page is a component): Inject IHttpContextAccessor and from it access HttpContext and then User; need to register IHttpContextAccessor in Startup.ConfigureServices, normally using AddHttpContextAccessor. Edit: according to the Microsoft docs you must not do this for security reasons. Inject an AuthenticationStateProvider property, call GetAuthenticationStateAsync … Read more

How do I get the access token from a blazor (server-side) web app?

The following code snippets provide a way to retrieve the access token issued when a user is authenticated with IdentityServer4 provider. In order to get the access token you can use the HttpContext object, but since Blazor is SignalR-based, you’ll have to do it the only time the HttpContext object is available, when the connection … Read more

How to use the HttpContext object in server-side Blazor to retrieve information about the user, user agent

The fiction that the HttpContext object can’t be used with Blazor Server App, has been long propagated on Stackoverflow, and it is high time to pension it off. It is true that the HttpContext is not available when a WebSocket connection is in operation, but this must be clear: When you type an url and … Read more