dotnet / dotnet/aspnetcore

Reduce boiler plate code for getting the current user in a Blazor component

Open
#55,327 9 comments 2 reactions 0 assignees View on GitHub
area-blazor enhancement Pillar: Dev Experience
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

In a Blazor component if you want to get the current user you typically have to do something like this:

```csharp
protected override async Task OnInitializedAsync()
{
var authenticationState = await AuthenticationStateTask;
CurrentUser = authenticationState.User;
}

[CascadingParameter]
public Task AuthenticationStateTask { get; set; } = default!;

public ClaimsPrincipal CurrentUser { get; set; } = default!
```

There are several things going on here: a special cascading parameter, overriding a component lifecycle event, and async logic to get the auth state. It's quite a bit more complicated than the convenient `User` property we have in MVC & Razor Pages. And you need this code wherever you want access to the current user. Is there something we could do to cut down on the amount of boiler plate code to get the current user?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.