System.Web.UI.Page won't let me access CurrentUser or User.Identity from a Controller in ASP.Net-MVC

后端 未结 3 735
眼角桃花
眼角桃花 2020-12-22 10:16

When I try

user = System.Web.UI.Page.CurrentUser

or

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

I get an erro

相关标签:
3条回答
  • 2020-12-22 10:36

    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.

    0 讨论(0)
  • 2020-12-22 10:43

    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?

    0 讨论(0)
  • 2020-12-22 10:44

    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;
    
    0 讨论(0)
提交回复
热议问题