There are two simple ways to get current user in MVC 5.
i. From controller class one can fetch current user id as follows:
string userId = User.Identity.GetUserId();
In that case one have to add following namespace
ii. If one want to fetch user id outside of a controller class he/she will use “HttpContext” class to fetch the user id.
In that case will use following code:
string userId = HttpContext.Current.User.Identity.GetUserId();
And of course don’t forget to add namespace
Hope above information will help us.
CSM, CSPO, CSD, CSP-SM, CSP-PO (ScrumAlliance.org)
Certification Profile Link-
Currently working as Lead Team (Application Architecture) at Raven Systems Ltd. Passion for software development especially agile practices such as TDD with in depth knowledge of Object Oriented Programming, SOLID Principles, Gang of Four Design Patterns, Some Enterprise Application Architectural Patterns. Over 8 years of software development experience ASP.NET. Has the ability to understand and transform complex business requirements into software ensuring applications are delivered on time. Also experience in non Microsoft .NET technologies such as Dapper.Net, Git, Structure Map & Angular, Bootstrap, HTML-5, CSS-3 etc.