C
C#•6mo ago
IcyIme

identity blazor

i have this code for getting user id of current user but its return null
csharp protected override async Task OnParametersSetAsync()
{
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
var user = authState.User;

if (string.IsNullOrEmpty(userId) && user.Identity.IsAuthenticated)
{
var userIdClaim = user.FindFirst(c => c.Type == "sub");
userId = userIdClaim?.Value;
}

await LoadProfileData();
}
csharp protected override async Task OnParametersSetAsync()
{
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
var user = authState.User;

if (string.IsNullOrEmpty(userId) && user.Identity.IsAuthenticated)
{
var userIdClaim = user.FindFirst(c => c.Type == "sub");
userId = userIdClaim?.Value;
}

await LoadProfileData();
}
i use blazor wasm and web api
2 Replies
danyule
danyule•6mo ago
Do you need to use the AuthenticationStateProvider? See here: https://learn.microsoft.com/en-us/aspnet/core/blazor/security/?view=aspnetcore-8.0#authenticationstateprovider-service
You don't typically use AuthenticationStateProvider directly. Use the AuthorizeView component or Task<AuthenticationState> approaches described later in this article. The main drawback to using AuthenticationStateProvider directly is that the component isn't notified automatically if the underlying authentication state data changes.
IcyIme
IcyImeOP•6mo ago
I just want get id of current user 😭 but it's not work If you want I will send u full project of git repo
Want results from more Discord servers?
Add your server