如何使用ASP.NET Core RC2 MVC6和IIS7获取当前的Windows用户 [英] How to get the current Windows user with ASP.NET Core RC2 MVC6 and IIS7

查看:190
本文介绍了如何使用ASP.NET Core RC2 MVC6和IIS7获取当前的Windows用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an intranet site built in MVC6 using ASP.NET Core RC2. I want to get the Windows username of the person accessing the intranet site.

因此,如果Jim转到Intranet网站,我希望该站点接收 Domain\Jim ,而如果安妮转到Intranet网站,我希望该网站接收 Domain\Anne。

So if Jim goes to the intranet site I want the site to receive "Domain\Jim", while if Anne goes to the intranet site I want the site to receive "Domain\Anne".

在我的IIS服务器上仅启用Windows身份验证,而匿名身份验证是

Only Windows Authentication is enabled on my IIS server, Anonymous Authentication is disabled.

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

My site is set to use Windows authentication and to disable anonymous authentication.

<system.web>
    <authentication mode="Windows" />
    <authorization>
        <deny users="?"/>
    </authorization>
</system.web>

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

Through Debug I can use System.Security.Principal.WindowsIdentity.GetCurrent().Name, but that of course returns "IIS APPPOOL\SiteName" on the IIS server.

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

I found many examples from older version of ASP.NET using HttpContext, and I tried injecting that into my controller with the following but userName ends up null.

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

//HomeController.cs
public HomeController(IHttpContextAccessor _accessor)
{
    string userName = _accessor.HttpContext.User.Identity.Name;
}

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

What is the correct way to pass the Windows username to an intranet site in ASP.NET Core RC2 MVC6?

推荐答案

可能会帮助仍在寻找的人。来自MS的每个文档 https:// docs.microsoft.com/zh-CN/aspnet/core/fundamentals/http-context?view=aspnetcore-2.2 ,在ConfigureServices方法(startup.cs)中添加HttpContextAccessor
将此httpcontextaccessor传递到控制器并访问已登录的用户。

Might help someone who are still looking for. Per documentation from MS https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-context?view=aspnetcore-2.2 , add the HttpContextAccessor in ConfigureServices method (startup.cs) Pass this httpcontextaccessor into the controller and access the logged in user.

我遵循了这个步骤,即使将核心网站部署到IIS之后,我仍然可以获得登录的Windows Authenticated用户。

I followed this and I was able to get the logged in Windows Authenticated user even after deploying the core website to IIS.

这是代码段

在Startup.cs中(在ConfigureServices方法下)添加行

In Startup.cs (under ConfigureServices method) add the line

services.AddHttpContextAccessor();

然后在您的控制器文件中,添加以下必要的代码行。下面的代码是一个示例。

Then in your controller file, add following piece of necessary code lines. The code below is a sample.

public class YourController : Controller
{
  private readonly IHttpContextAccessor _httpContextAccessor;
  private readonly string _user;
  public YourController(IHttpContextAccessor httpContextAccessor)
  {
    _httpContextAccessor = httpContextAccessor;
    _user = _httpContextAccessor.HttpContext.User.Identity.Name;
  }
}

这篇关于如何使用ASP.NET Core RC2 MVC6和IIS7获取当前的Windows用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆