How to get the current user in ASP.NET MVC


Question

In a forms model, I used to get the current logged-in user by:

Page.CurrentUser

How do I get the current user inside a controller class in ASP.NET MVC?

1
261
1/11/2017 6:41:34 PM

Accepted Answer

If you need to get the user from within the controller, use the User property of Controller. If you need it from the view, I would populate what you specifically need in the ViewData, or you could just call User as I think it's a property of ViewPage.

256
7/11/2015 5:55:25 AM

I found that User works, that is, User.Identity.Name or User.IsInRole("Administrator").


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon