在表单模型中,我曾经通过以下方式获取当前登录用户:
Page.CurrentUser
如何在ASP中获得控制器类中的当前用户。净MVC吗?
在表单模型中,我曾经通过以下方式获取当前登录用户:
Page.CurrentUser
如何在ASP中获得控制器类中的当前用户。净MVC吗?
当前回答
我发现User工作,即User. identity . name或User. isinrole(“管理员”)。
其他回答
我发现User工作,即User. identity . name或User. isinrole(“管理员”)。
为了引用使用ASP中内置的简单身份验证创建的用户ID。如果你使用数据库优先和实体框架5来生成代码优先绑定,并且你的表是结构化的,因此使用userID的外键,这是很有帮助的),你可以使用
WebSecurity.CurrentUserId
一旦你添加了using语句
using System.Web.Security;
在IIS管理器的“身份验证”下,禁用: 1)匿名认证 2)表单认证
然后添加以下到你的控制器,以处理测试和服务器部署:
string sUserName = null;
string url = Request.Url.ToString();
if (url.Contains("localhost"))
sUserName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
else
sUserName = User.Identity.Name;
你可以使用以下代码:
Request.LogonUserIdentity.Name;
IPrincipal currentUser = HttpContext.Current.User;
bool writeEnable = currentUser.IsInRole("Administrator") ||
...
currentUser.IsInRole("Operator");