asp.net-mvc 在 asp.net mvc 请求中捕获 windows 用户名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2132807/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:05:24  来源:igfitidea点击:

capture windows username in asp.net mvc request

asp.net-mvc

提问by leora

i have an intranet app asp.net mvc site. is there anyway to capture the windows nt login from the user wihtout having a full login system in the site. i dont want permissioning but i would like to do some logging on the server to track requests, etc . .

我有一个 Intranet 应用程序 asp.net mvc 站点。无论如何,是否可以在站点中没有完整登录系统的情况下从用户那里捕获 windows nt 登录信息。我不想获得许可,但我想在服务器上进行一些日志记录以跟踪请求等。.

回答by Dan Diplo

You can use HttpContext.Current.User.Identity.Nameif you set up the site to use Windows Authentication. Many browsers will pass the username on transparently.

您可以使用HttpContext.Current.User.Identity.Name,如果你设置为使用该网站的Windows身份验证。许多浏览器会透明地传递用户名。

回答by James Toomey

Go to the Web.Config file for your application (be sure it's the main one, not the Web.Config under Views), remark out <authentication mode="Forms">if you see it, then add lines like this:

转到您的应用程序的 Web.Config 文件(确保它是主要文件,而不是 Views 下的 Web.Config),<authentication mode="Forms">如果您看到它,请备注,然后添加如下几行:

<authentication mode="Windows"/>
<identity impersonate="true"/>
<authorization>
  <allow roles="DOMAIN\PersonnelGroup" />
  <allow users="DOMAIN\jdoe"/>
  <deny users="*"/>
</authorization>

You don't have to include the <authorization>section but it's useful if you want to allow access for certain groups and users, and deny everyone else. When you use IE, the credentials get passed along automatically. You can test this by printing the username in your view:

您不必包含该<authorization>部分,但如果您希望允许某些组和用户访问并拒绝其他所有人,它会很有用。当您使用 IE 时,凭据会自动传递。您可以通过在视图中打印用户名来测试:

@HttpContext.Current.User.Identity.Name

If you open your site using Firefox or any other browser besides IE, it will prompt you for the username and password because it's not automatically passing along the credentials (although apparently you can get Firefox to pass along the credentials, as Dan Diplo mentions above).

如果您使用 Firefox 或除 IE 之外的任何其他浏览器打开您的网站,它会提示您输入用户名和密码,因为它不会自动传递凭据(尽管显然您可以让 Firefox 传递凭据,正如 Dan Diplo 上面提到的那样) .

If you want to pass these credentials along to another server, such as a SQLServer, it becomes more of a headache from my experience, because you run into a double hop issue. The only workaround I know of is to host IIS and SqlServer on the same server, or have a login for your intranet so you have the username and password to pass along to SQLServer.

如果您想将这些凭据传递给另一台服务器,例如 SQLServer,根据我的经验,这会变得更令人头疼,因为您遇到了双跳问题。我所知道的唯一解决方法是将 IIS 和 SqlServer 托管在同一台服务器上,或者登录到您的 Intranet,以便您将用户名和密码传递给 SQLServer。

回答by IanT8

What version of Windows is this mvc app running on?

这个 mvc 应用程序在哪个版本的 Windows 上运行?

You could try the following:

您可以尝试以下操作:

  • Use IIS Manager to disable anonymous access to the site, and make sure digest and/or windows authentication is enabled.
  • Change the mvc app's web.config to use Windows Authentication
  • Access the login name in the controller using User.Identity.Name.
  • 使用 IIS 管理器禁用对站点的匿名访问,并确保启用摘要和/或 Windows 身份验证。
  • 更改 mvc 应用程序的 web.config 以使用 Windows 身份验证
  • 使用 User.Identity.Name 访问控制器中的登录名。

Would that do?

那行吗?

回答by Diego

try this.

尝试这个。

 var user = System.Security.Principal.WindowsIdentity.GetCurrent().Name;

it returns Domail/User

它返回 Domail/User

Do not forget to put this in web.Config

不要忘记把它放在 web.Config 中

<identity impersonate="true"/> 

回答by Admiral Land

You can try to fetch user's name with the following piece of code:

您可以尝试使用以下代码获取用户名:

var userName = HttpContext.User.Identity.Name;

User.Identityis automatically populated if you use any authentication (example: cookie based, etc)

User.Identity如果您使用任何身份验证(例如:基于 cookie 等),则会自动填充

回答by blowdart

If you set IIS to use Windows Authentication and the site is in the Intranet Zone for your clients then the logins will automatically happen without any prompts at all.

如果您将 IIS 设置为使用 Windows 身份验证,并且该站点位于您的客户端的 Intranet 区域中,则登录将自动进行,根本没有任何提示。