从MVC Web应用程序的MVC控制器获取用户名的代码是什么

问题描述 投票:0回答:1

我正在尝试在发布到IIS的MVC Web应用程序中获取用户名,但是它不起作用。这些是我尝试过的三件事:

1)public string userName = System.Environment.UserName;

2)public string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;

3)在视图中:@Html.Label((string)ViewBag.Name)

在控制器中:ViewBag.Name = userName;

当我在计算机上本地调试应用程序时,它可以工作。虽然,当我访问该网站时并没有获得我的用户名,而是从IIS中获得了一些东西?我只是想在网站上获取此人的用户名,而没有其他功能。我添加了一张IIS身份验证的照片,我不知道这是否重要,但是我确保我不会丢失任何东西。你能帮我这个忙吗?提前非常感谢您。enter image description here

javascript c# html model-view-controller
1个回答
0
投票

在您的控制器中,要获取用户名,可以使用此:

var userName = Request.RequestContext.HttpContext.User.Identity.Name

© www.soinside.com 2019 - 2024. All rights reserved.