asp.net-mvc 在asp.net中,当身份模拟=“true”时如何获取Windows用户名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1267071/
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
How to get Windows user name when identity impersonate="true" in asp.net?
提问by MrDustpan
I'm creating an intranet asp.net mvc application that everyone in the company should have access to. I need to run the website impersonated for database access etc., but I want to know who each user is.
我正在创建一个 Intranet asp.net mvc 应用程序,公司中的每个人都应该可以访问它。我需要运行模拟数据库访问等的网站,但我想知道每个用户是谁。
When I look at Page.User.Identity.Nameit's blank. Is it possible to get the user's windows account name even though the site is running impersonated?
当我看Page.User.Identity.Name它时,它是空白的。即使站点正在模拟运行,是否也可以获得用户的 Windows 帐户名?
Edit:Here's a little more info. I have a site in IIS 6 running with anonymous access enabled. The site is running under a system account that has access to the database (because all of the employees do not have access to the database).
编辑:这里有更多信息。我在 IIS 6 中有一个启用匿名访问的站点。该站点在有权访问数据库的系统帐户下运行(因为所有员工都没有访问数据库的权限)。
My web.config has <authentication mode="Windows" />and <identity impersonate="true"/>
我的 web.config 有<authentication mode="Windows" />和<identity impersonate="true"/>
My goal is that the users won't have to log in - that fact that they are logged into our network (and the fact that the site is not on an external IP) is enough authentication. I would just like to know who the user is in order to track changes they make, etc.
我的目标是用户不必登录——他们登录到我们的网络这一事实(以及该站点不在外部 IP 上的事实)就足以进行身份验证。我只想知道用户是谁,以便跟踪他们所做的更改等。
回答by gokul
With <authentication mode="Windows"/>in your application and Anonymous access enabled in IIS, you will see the following results:
随着<authentication mode="Windows"/>您的应用程序和匿名访问IIS中启用,您将看到以下结果:
System.Environment.UserName: Computer Name
Page.User.Identity.Name: Blank
System.Security.Principal.WindowsIdentity.GetCurrent().Name: Computer Name
With <authentication mode="Windows"/>in your application, and ‘Anonymous access' disabled and only ‘Integrated Windows Authentication' in IIS, you will see the following results:
随着<authentication mode="Windows"/>在你的应用程序,而“匿名访问”禁用的,只有“集成Windows身份验证”在IIS中,您将看到以下结果:
System.Environment.UserName: ASPNET (user account used to run ASP.NET service)
Page.User.Identity.Name: Domain\ Windows Account Name
System.Security.Principal.WindowsIdentity.GetCurrent().Name: Computer Name\ASPNET
With <authentication mode="Windows"/>and <identity impersonate ="true"/>in your application, and ‘Anonymous access' disabled and only ‘Integrated Windows Authentication' in IIS, you will see the following results:
使用<authentication mode="Windows"/>和<identity impersonate ="true"/>在您的应用程序中,并且在 IIS 中禁用“匿名访问”并仅启用“集成 Windows 身份验证”,您将看到以下结果:
System.Environment.UserName: Windows Account Name
Page.User.Identity.Name: Domain\ Windows Account Name
System.Security.Principal.WindowsIdentity.GetCurrent().Name: Domain\ Windows Account Name
回答by Gavin
try this
尝试这个
System.Security.Principal.WindowsIdentity.GetCurrent().Name
It should return a string with the users login name
它应该返回一个带有用户登录名的字符串
回答by Kelly R
I just wanted to post my fix, because no one else had said anything about it.
我只是想发布我的修复程序,因为没有其他人对此发表任何评论。
I was having the same issue when I published the site to the server, but not on my local. All the setting were the same. However, in IIS the "Default Website" had never been turned off. It was running and intercepting traffic, even though there was no site associated with it. Anonymous Authentication was turned on in the default, but turned off in my website running under port 80. It didn't seem to matter that my site had it turned off... since the default was turned on it was turned on for all traffic to port 80.
当我将站点发布到服务器时,我遇到了同样的问题,但不在我的本地。所有的设置都是一样的。但是,在 IIS 中,“默认网站”从未被关闭。它正在运行并拦截流量,即使没有与之关联的站点。匿名身份验证在默认情况下是打开的,但在我的网站上在端口 80 下运行时关闭。我的网站关闭它似乎无关紧要......因为默认设置是打开的,它对所有流量都打开到 80 端口。
Disabling the default web fixed the issue. Also changing the port to 8080 works.
禁用默认 Web 修复了该问题。将端口更改为 8080 也有效。
I hope this helps someone.
我希望这可以帮助别人。
回答by Ryan
Unless this functionality has changed under the MVC framework, and I don't think it has, Page.User.Identity.Name should still work. Sounds like your site is set up to allow anonymous authentication. If so, try disabling it.
除非这个功能在 MVC 框架下发生了变化,而且我认为它没有发生变化,否则 Page.User.Identity.Name 应该仍然有效。听起来您的网站设置为允许匿名身份验证。如果是这样,请尝试禁用它。

