如何获得流用户在ASP.NET 视
-
06-07-2019 - |
题
在形式的模型,我用得到目前登录用户:
Page.CurrentUser
我怎么当前用户的内部控制器类ASP.NET 视?
解决方案
如果你需要得到用户的内部控制器,使用 User
财产的控制器。如果你需要它从来看,我会填充有什么特别需要的 ViewData
, 或者你可以只叫用户,因为我认为这是一个酒店的 ViewPage
.
其他提示
我发现 User
有效,即 User.Identity.Name
或 User.IsInRole(" Administrator")
。
尝试 HttpContext.Current.User
。
Public Shared Property Current()As System.Web.HttpContext结果 System.Web.HttpContext的成员
总结:结果 获取或设置当前HTTP请求的System.Web.HttpContext对象。
返回值:
当前的System.Web.HttpContext HTTP请求
您可以在ASP.NET MVC4中获取用户名,如下所示:
System.Web.HttpContext.Current.User.Identity.Name
我知道这是真的老,但我才刚开始ASP.NET 视,所以我想我会坚持我的两个分:
Request.IsAuthenticated
告诉你如果用户进行身份验证。Page.User.Identity
给你的身份登录用户。
我用:
Membership.GetUser().UserName
我不确定这会在ASP.NET MVC中有效,但值得一试:)
登录用户名: System.Web.HttpContext.Current.User.Identity.Name
为了在控制器中引用在ASP.NET MVC 4中内置的简单身份验证创建的用户ID以进行过滤(如果您首先使用数据库而实体框架5生成代码优先绑定和表格,这将非常有用)是结构化的,以便使用userID的外键),你可以使用
WebSecurity.CurrentUserId
添加使用声明后
using System.Web.Security;
UserName with:
User.Identity.Name
但是如果您只需要获取ID,则可以使用:
using Microsoft.AspNet.Identity;
因此,您可以直接获取用户ID:
User.Identity.GetUserId();
此页面可能是您要寻找的内容:
在MVC3中使用Page.User.Identity.Name
您只需要 User.Identity.Name
。
使用 System.Security.Principal.WindowsIdentity.GetCurrent()。Name
。
这将获得当前登录的Windows用户。
我们可以使用以下代码来获取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'
对于它的价值,在ASP.NET MVC 3中,你可以使用User来返回当前请求的用户。
如果您在登录页面内,例如在LoginUser_LoggedIn事件中,Current.User.Identity.Name将返回一个空值,因此您必须使用yourLoginControlName.UserName属性。
MembershipUser u = Membership.GetUser(LoginUser.UserName);
IPrincipal currentUser = HttpContext.Current.User;
bool writeEnable = currentUser.IsInRole("Administrator") ||
...
currentUser.IsInRole("Operator");
var ticket = FormsAuthentication.Decrypt(
HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName].Value);
if (ticket.Expired)
{
throw new InvalidOperationException("Ticket expired.");
}
IPrincipal user = (System.Security.Principal.IPrincipal) new RolePrincipal(new FormsIdentity(ticket));
如果您正在Intranet上的Active Directory中工作,请参阅以下提示:
(Windows Server 2012)
在Web服务器上运行与AD对话的任何内容都需要大量的更改和耐心。因为当在Web服务器上运行而不是本地IIS / IIS Express时,它以AppPool的身份运行,因此,您必须将其设置为模拟访问该站点的任何人。
当ASP.NET MVC应用程序在网络内的Web服务器上运行时,如何将当前登录用户置于活动目录中:
// Find currently logged in user
UserPrincipal adUser = null;
using (HostingEnvironment.Impersonate())
{
var userContext = System.Web.HttpContext.Current.User.Identity;
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, ConfigurationManager.AppSettings["AllowedDomain"], null,
ContextOptions.Negotiate | ContextOptions.SecureSocketLayer);
adUser = UserPrincipal.FindByIdentity(ctx, userContext.Name);
}
//Then work with 'adUser' from here...
您必须在以下内容中包含与“活动目录上下文”有关的任何调用,以便它充当获取AD信息的托管环境:
using (HostingEnvironment.Impersonate()){ ... }
您必须在web.config中将 impersonate
设置为true:
<system.web>
<identity impersonate="true" />
您必须在web.config中启用Windows身份验证:
<authentication mode="Windows" />
您可以使用以下代码:
Request.LogonUserIdentity.Name;
在Asp.net Mvc Identity 2中,您可以通过以下方式获取当前用户名:
var username = System.Web.HttpContext.Current.User.Identity.Name;
在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;