Home Articles Contact Us Client Area Register Login

How Do I get the Current UserId in an ASP.NET Core Identity View or Controller

Mark Pringle - Sunday, November 13, 2022

Accessing the current UserId (ASPNetUsers Id) of an ASP.NET Core Identity application is essential to creating user-specific applications and filtering data based on a specific user.

When users create (register) an account in an ASP.NET Core Identity application, a distinct user ID is generated and added to the SQL Server database ASPNetUsers' table. This table was created when migrations were applied to initialize the database. This database table also stores profile data, passwords, e-mail addresses, and other personal data. Generally speaking, ASP.NET Core Identity adds user interface login and role functionality to ASP.NET Core web apps.

ASPNetUsers Database

How do you get the current UserId in a view or controller?

Get current user id:

User.FindFirstValue(ClaimTypes.NameIdentifier)

Namespace:

using System.Security.Claims;

How you access it in a view:

Get UserId in a View

How the ASPNetUsers Id looks rendered in a browser:

rendered userid

What it looks like in a controller

UserID in a Controller