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

 
Written by Mark Pringle |
Updated:

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

Copyright © TravelDailyLife.com

Author: Mark Pringle
I'm just a guy who is addicted to the unfamiliar and who fulfills this addiction by traveling and writing about my travels. As a lover of sports, penning opinion articles related to sports is also a pastime.
My External Website (External Website Opens in New Window)

Comments

Please Login to Comment
No comments have been posted. Be the first.



Hire a Writer