如何在ASP.NET MVC的局部视图中获取用户(IPrincipal)?

4
在一个System.Web.UI.Page.ViewPage中,我可以访问当前用户(System.Security.Principal.IPrincipal),但是在System.Web.UI.UserControl.ViewUserControl中,我无法访问用户(因为ViewPage属性是internal)。我的部分视图(.ascx)如何根据IPrincipal显示信息?
3个回答

11
ViewContext.HttpContext.User

4
HttpContext.Current.User

3
如果你这样做,就会破坏单元测试——使用ViewContext.HttpContext.*,它使用HttpContextBase并且是为了单元测试而设计的。 - Ethan J. Brown

3
你可以通过ViewContext访问它。
ViewContext.HttpContext.User

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接