[英]System.Web.UI.Page won't let me access CurrentUser or User.Identity from a Controller in ASP.Net-MVC
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 我收到一条错误消息,指出未为System.Web.UI.Page定义该方法
I am trying to access it within a Controller, does that matter? 我正在尝试在Controller中访问它,这有关系吗?
I checked that I do not have another class named Page, Why would it say the method is not defined? 我检查了是否没有另一个名为Page的类,为什么会说该方法未定义?
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. Page.User
当有一个属性可在Page
多数民众赞成处理当前请求的HTTP处理程序。 As in an MVC controller, the request has not been handed to a Page
class, it won't work. 与在MVC控制器中一样,该请求尚未传递给
Page
类,因此无法正常工作。
in the controller. 在控制器中。
Edit: Looks like someone else beat me to it. 编辑:好像有人击败了我。
Found the answer here: ASP.NET Controller Base Class User.Identity.Name 在这里找到答案: ASP.NET控制器基类User.Identity.Name
HttpContext.User
seemed to work alright... anyone see anything wrong with that? HttpContext.User
似乎工作正常……有人看到有什么问题吗?
CurrentUser doesn't seem to be a property on System.Web.UI.Page. CurrentUser似乎不是System.Web.UI.Page的属性。 ( http://msdn.microsoft.com/en-us/library/system.web.ui.page.aspx )
( http://msdn.microsoft.com/en-us/library/system.web.ui.page.aspx )
Your controller should expose a property named User, does that work? 您的控制器应公开一个名为User的属性,这行得通吗? For example:
例如:
var user = User;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.