简体   繁体   English

System.Web.UI.Page不允许我从ASP.Net-MVC中的控制器访问CurrentUser或User.Identity

[英]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.

相关问题 在ASP.NET MVC5中等效的系统类(System.Web.UI.Page)属性(httpresponse) - system class(System.Web.UI.Page) property(httpresponse) equivalent in asp.net mvc5 ASP.NET MVC母版页:“&#39;Model&#39;不是&#39;System.Web.UI.Page&#39;的成员” - ASP.NET MVC Master Page: “'Model' is not a member of 'System.Web.UI.Page'” ASP._page_views_diagnostics_<page> _cshtml 不继承自 System.web.ui.page</page> - ASP._page_views_diagnostics_<page>_cshtml does not inherit from System.web.ui.page 模拟ASP.NET MVC中的User.Identity - Mocking User.Identity in ASP.NET MVC 错误:类型“ ASP._Page_views_index_cshtml”没有从“ System.Web.UI.Page”继承 - Error:Type 'ASP._Page_views_index_cshtml' does not inherit from 'System.Web.UI.Page' 为什么Bootstrap不让我在ASP.NET MVC页面的下拉菜单中设置边距? - Why won't Bootstrap let me set margins on dropdowns in ASP.NET MVC page? 为什么System.Web.HttpContext.Current.User在控制器构造函数期间解析,但User.Identity不能解析 - Why does System.Web.HttpContext.Current.User resolve during a controller constructor, but User.Identity does not 如何向 ASP.NET MVC user.Identity 添加更多用户数据 - How to add more user data to ASP.NET MVC user.Identity ASP.NET-MVC。 如何从URL获取控制器名称? - ASP.NET-MVC . How to get the controller name from an url? ASP.NET Core 2.2 User.Identity在剃刀中有一个值,但在控制器中没有 - ASP.NET Core 2.2 User.Identity has a value in razor but not in controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM