0

When I try

user = System.Web.UI.Page.CurrentUser

or

user = System.Web.UI.Page.User.Identity

I get an error saying that the method is not defined for System.Web.UI.Page

I am trying to access it within a Controller, does that matter?

I checked that I do not have another class named Page, Why would it say the method is not defined?

niton
  • 8,771
  • 21
  • 32
  • 52
Matt
  • 5,249
  • 12
  • 40
  • 45

3 Answers3

7

There are many ways to do it (basically, they are all the same)

User.Identity // in the controller
HttpContext.User.Identity // in the controller
System.Web.HttpContext.Current.User.Identity // anywhere

Page.User property works when there's a Page HTTP handler that's processing the current request. As in an MVC controller, the request has not been handed to a Page class, it won't work.

in the controller.

Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789
0

Edit: Looks like someone else beat me to it.

Found the answer here: ASP.NET Controller Base Class User.Identity.Name
HttpContext.User seemed to work alright... anyone see anything wrong with that?

Community
  • 1
  • 1
Matt
  • 5,249
  • 12
  • 40
  • 45
0

CurrentUser doesn't seem to be a property on System.Web.UI.Page. (http://msdn.microsoft.com/en-us/library/system.web.ui.page.aspx)

Your controller should expose a property named User, does that work? For example:

var user = User;
nikmd23
  • 9,095
  • 4
  • 42
  • 57