© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
21 replies
Duch Jessie

✅ 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";
        }
    }
}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ .Net Core API User.FindFirstValue(ClaimTypes.NameIdentifier) in every action?!
C#CC# / help
4y ago
❔ how to use ClaimTypes
C#CC# / help
4y ago
❔ Claim type of JwtRegisteredClaimNames.Name unrecognized as ClaimTypes.Name
C#CC# / help
4y ago
User ID on Client Side or Not?
C#CC# / help
13mo ago