Written by Mark Pringle | Last Updated on Sunday, November 13, 2022
Authentication ASP.NET Version: 6.0 General Information
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.
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:
How the ASPNetUsers Id looks rendered in a browser:
What it looks like in a controller