✅ ClaimTypes.NameIdentifier not retruning id of current user
i want to get id of current user but ClaimTypes.NameIdentifier not working and i dont know why
@page "/profile"
@using System.Security.Claims
@using Microsoft.AspNetCore.Authorization
@using UniCademy.Client.Model
@inject AuthenticationStateProvider AuthenticationStateProvider
@attribute [Authorize]
<PageTitle>Profile</PageTitle>
<h1>@name</h1>
<h2>@userId</h2>
@code {
private string name;
private string userId;
protected override async Task OnInitializedAsync()
{
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
var user = authState.User;
if (user.Identity.IsAuthenticated)
{
name = user.Identity.Name;
userId = user.FindFirst(c => c.Type == ClaimTypes.NameIdentifier)?.Value;
}
else
{
name = "User not authenticated";
userId = "No ID";
}
}
}@page "/profile"
@using System.Security.Claims
@using Microsoft.AspNetCore.Authorization
@using UniCademy.Client.Model
@inject AuthenticationStateProvider AuthenticationStateProvider
@attribute [Authorize]
<PageTitle>Profile</PageTitle>
<h1>@name</h1>
<h2>@userId</h2>
@code {
private string name;
private string userId;
protected override async Task OnInitializedAsync()
{
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
var user = authState.User;
if (user.Identity.IsAuthenticated)
{
name = user.Identity.Name;
userId = user.FindFirst(c => c.Type == ClaimTypes.NameIdentifier)?.Value;
}
else
{
name = "User not authenticated";
userId = "No ID";
}
}
}