相关疑难解决方法(0)

如何使用ASP.NET Core RC2 MVC6和IIS7获取当前Windows用户

我有一个使用ASP.NET Core RC2在MVC6中构建的Intranet站点.我想获取访问Intranet站点的人的Windows用户名.

因此,如果Jim访问内部网站点,我希望该站点接收"Domain\Jim",而如果Anne访问内部网站,我希望该站点接收"Domain\Anne".

我的IIS服务器上仅启用了Windows身份验证,禁用了匿名身份验证.

我的网站设置为使用Windows身份验证并禁用匿名身份验证.

<system.web>
    <authentication mode="Windows" />
    <authorization>
        <deny users="?"/>
    </authorization>
</system.web>
Run Code Online (Sandbox Code Playgroud)

通过调试我可以使用System.Security.Principal.WindowsIdentity.GetCurrent().Name,但当然在IIS服务器上返回"IIS APPPOOL\SiteName".

我使用HttpContext从旧版本的ASP.NET中找到了许多示例,我尝试使用以下内容将其注入到我的控制器中,但userName最终为null.

//Startup.cs
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

//HomeController.cs
public HomeController(IHttpContextAccessor _accessor)
{
    string userName = _accessor.HttpContext.User.Identity.Name;
}
Run Code Online (Sandbox Code Playgroud)

将Windows用户名传递给ASP.NET Core RC2 MVC6中的Intranet站点的正确方法是什么?

c# iis-7 asp.net-core-mvc asp.net-core .net-core-rc2

10
推荐指数
2
解决办法
2万
查看次数