![](/img/trans.png)
[英]How to I get the username of the currently logged in user in ASP.NET Core 2, not Application Pool details
[英]Get the logged username to ASP.net application
我有这个应用程序,我需要获取 windows 登录用户的名称。 我将它托管在服务器上。 当我在本地进行测试时,这工作得很好,但是当我托管在服务器上时,程序返回服务器名称,而不是登录的用户用户名。 我的代码在下面提到
索引.cshtml
<h1>Hello @ViewBag.UserName</h1>
Controller
public ActionResult Index()
{
string name = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
string x = "";
x = name.Split('\\')[1];
ViewBag.UserName = x;
return View();
}
Web.config
<system.web>
<compilation debug="true" targetFramework="4.8" />
<httpRuntime targetFramework="4.8" />
<authentication mode="Windows"/>
</system.web>
任何帮助表示赞赏。 谢谢你。
由于您在.Net 框架中使用 MVC,因此您可以使用以下方法获取用户的名称/身份:
string name = User.Identity.Name;
它返回服务器的原因是WindowsIdentity.GetCurrent()
返回运行线程的标识,而User.Identity
返回传递给 IIS 的标识。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.