在表单模型中,我曾经通过以下方式获取当前登录用户:
Page.CurrentUser
如何在ASP中获得控制器类中的当前用户。净MVC吗?
在表单模型中,我曾经通过以下方式获取当前登录用户:
Page.CurrentUser
如何在ASP中获得控制器类中的当前用户。净MVC吗?
当前回答
我使用:
Membership.GetUser().UserName
我不确定这是否适用于ASP。NET MVC,但它值得一试:)
其他回答
这个页面可能就是你要找的: 在MVC3中使用Page.User.Identity.Name
你只需要User.Identity.Name。
IPrincipal currentUser = HttpContext.Current.User;
bool writeEnable = currentUser.IsInRole("Administrator") ||
...
currentUser.IsInRole("Operator");
为了引用使用ASP中内置的简单身份验证创建的用户ID。如果你使用数据库优先和实体框架5来生成代码优先绑定,并且你的表是结构化的,因此使用userID的外键,这是很有帮助的),你可以使用
WebSecurity.CurrentUserId
一旦你添加了using语句
using System.Web.Security;
我们可以使用下面的代码在ASP中获取当前登录的用户。Net MVC:
var user= System.Web.HttpContext.Current.User.Identity.GetUserName();
也
var userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name; //will give 'Domain//UserName'
Environment.UserName - Will Display format : 'Username'
如果有人还在读这篇文章,我用下面的方式访问cshtml文件。
<li>Hello @User.Identity.Name</li>