[英]How to get Windows user name when identity impersonate=“true” in asp.net?
I'm creating an intranet asp.net mvc application that everyone in the company should have access to. 我正在创建一个内部网asp.net mvc应用程序,公司中的每个人都应该可以访问它。 I need to run the website impersonated for database access etc., but I want to know who each user is.
我需要运行模拟数据库访问的网站等,但我想知道每个用户是谁。
When I look at Page.User.Identity.Name
it's blank. 当我看到
Page.User.Identity.Name
它是空白的。 Is it possible to get the user's windows account name even though the site is running impersonated? 即使网站正在模拟运行,是否可以获取用户的Windows帐户名称?
Edit: Here's a little more info. 编辑:这里有更多信息。 I have a site in IIS 6 running with anonymous access enabled.
我在IIS 6中有一个运行匿名访问的站点。 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).
该站点在可以访问数据库的系统帐户下运行(因为所有员工都无权访问数据库)。
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. 我的目标是用户不必登录 - 他们登录我们网络的事实(以及该网站不在外部IP上的事实)是足够的身份验证。 I would just like to know who the user is in order to track changes they make, etc.
我想知道用户是谁以跟踪他们所做的更改等。
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"/>
,并且在IIS中禁用“匿名访问”并且仅“集成Windows身份验证”,您将看到以下结果:
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
try this 试试这个
System.Security.Principal.WindowsIdentity.GetCurrent().Name
It should return a string with the users login name 它应该返回一个包含用户登录名的字符串
Unless this functionality has changed under the MVC framework, and I don't think it has, Page.User.Identity.Name should still work. 除非MVC框架下的这个功能已经改变,并且我认为它没有,否则Page.User.Identity.Name仍然可以工作。 Sounds like your site is set up to allow anonymous authentication.
听起来您的网站设置为允许匿名身份验证。 If so, try disabling it.
如果是这样,请尝试禁用它。
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.
但是,在IIS中,“默认网站”从未被关闭过。 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.
匿名身份验证在默认情况下已打开,但在我的网站上运行在端口80下关闭。我的网站关闭它似乎并不重要...因为默认设置已打开,所以所有流量都打开了到80号港口。
Disabling the default web fixed the issue. 禁用默认Web修复了该问题。 Also changing the port to 8080 works.
也将端口更改为8080工作。
I hope this helps someone. 我希望这可以帮助别人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.