打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
关于 web.config impersonate 账号模拟

1、模拟 IIS 验证的帐户或用户

若要在收到 ASP.NET 应用程序中每个页的每个请求时模拟 Microsoft Internet 信息服务 (IIS) 身份验证用户,必须在此应用程序的 Web.config 文件中包含 <identity> 标记,并将 impersonate 属性设置为 true

2、为 ASP.NET 应用程序的所有请求模拟特定用户

    若要为 ASP.NET 应用程序的所有页面上的所有请求模拟特定用户,可以在该应用程序的 Web.config 文件的 <identity> 标记中指定 userName 和 password 属性。例如:
<identity impersonate="true" userName="accountname" password="password" />

IIS 匿名身份验证

Web.config 设置

变量位置

结果标识

<identity impersonate="true"/>
<authentication mode="Windows" />

HttpContext
WindowsIdentity
Thread

-
MACHINE\IUSR_MACHINE
-

<identity impersonate="false"/>
<authentication mode="Windows" />

HttpContext
WindowsIdentity
Thread

-
MACHINE\ASPNET
-

<identity impersonate="true" />
<authentication mode="Forms" />

HttpContext
WindowsIdentity
Thread

用户提供的名称
MACHINE\IUSR_MACHINE
用户提供的名称

<identity impersonate="false"/>
<authentication mode="Forms" />

HttpContext
WindowsIdentity
Thread

用户提供的名称
MACHINE\ASPNET
用户提供的名称

IIS 基本身份验证

Web.config 设置

变量位置

结果标识

<identity impersonate="true"/>
<authentication mode="Windows" />

HttpContext
WindowsIdentity
Thread

域\用户名
域\用户名
域\用户名

<identity impersonate="false"/>
<authentication mode="Windows" />

HttpContext
WindowsIdentity
Thread

域\用户名
MACHINE\ASPNET
域\用户名

<identity impersonate="true"/>
<authentication mode="Forms" />

HttpContext
WindowsIdentity
Thread

用户提供的名称
域\用户名
用户提供的名称

<identity impersonate="false"/>
<authentication mode="Forms" />

HttpContext
WindowsIdentity
Thread

用户提供的名称
MACHINE\ASPNET
用户提供的名称

IIS 摘要式身份验证

Web.config 设置

变量位置

结果标识

<identity impersonate="true"/>
<authentication mode="Windows" />

HttpContext
WindowsIdentity
Thread

域\用户名
域\用户名
域\用户名

<identity impersonate="false"/>
<authentication mode="Windows" />

HttpContext
WindowsIdentity
Thread

域\用户名
MACHINE\ASPNET
域\用户名

<identity impersonate="true"/>
<authentication mode="Forms" />

HttpContext
WindowsIdentity
Thread

用户提供的名称
域\用户名
用户提供的名称

<identity impersonate="false"/>
<authentication mode="Forms" />

HttpContext
WindowsIdentity
Thread

用户提供的名称
MACHINE\ASPNET
用户提供的名称

IIS 集成 Windows

Web.config 设置

变量位置

结果标识

<identity impersonate="true"/>
<authentication mode="Windows" />

HttpContext
WindowsIdentity
Thread

域\用户名
域\用户名
域\用户名

<identity impersonate="false"/>
<authentication mode="Windows" />

HttpContext
WindowsIdentity
Thread

域\用户名
MACHINE\ASPNET
域\用户名

<identity impersonate="true"/>
<authentication mode="Forms" />

HttpContext
WindowsIdentity
Thread

用户提供的名称
域\用户名
用户提供的名称

<identity impersonate="false"/>
<authentication mode="Forms" />

HttpContext
WindowsIdentity
Thread

用户提供的名称
MACHINE\ASPNET
用户提供的名称

以上各表说明了在 IIS 身份验证设置的范围内,从保存 IPrincipal 和/或 IIdentity 对象的每个变量中获取的结果标识。表中使用了以下缩写词:

·

HttpContext = HttpContext.Current.User,它返回包含当前 Web 请求的安全信息的 IPrincipal 对象。这是经身份验证的 Web 客户端。

·

WindowsIdentity = WindowsIdentity.GetCurrent(),它返回当前执行的 Win32 线程的安全性上下文的标识。

·

Thread = Thread.CurrentPrincipal,它返回当前执行的 .NET 线程(在 Win32 线程之上)的主体。

////////////////////////////////////////////////////////////////////////////////////////////
在Web.Config中有一个<identity>
元素,可以使其impersonate属性为true来设置本应用程序的身份,从而达到扮演其他账号身份的目的。而具体的“扮演”有两种方式:
(1)、<identity impersonate="true" />
这种方式的话ASP.NET进程将扮演http/https请求者的身份。而具体是什么账号,就跟IIS的安全性设定有关了:
如果IIS允许匿名访问,那么被扮演的将是Iuser_Machine账号(因为默认的匿名访问账号是Iuser_Machine,当然如果修改过,那被扮演的就是修改过的账号)。
如果IIS不允许匿名访问,那么浏览器请求者的身份肯定是一个Windows账号,这个账号就是被扮演的对象。

(2)、<identity impersonate="true" userName="Account" password="Password"/>
这种方式就直接设置所扮演的固定用户身份

第(2)种扮演方法将把password明文的写在Web.Config里,很不安全

PS1:微软不推荐使用扮演,无论扮演哪个账号,一方面是安全性问题(被扮演的账号可能有一些程序并不需要的多余权限,可能被利用),另一方面是可伸缩性问题(Scalability),因为在访问SQL Server等资源时,无法利用连接池等手段。

PS2:在\%windows%\Microsoft.NET\Framework\Version\Config目录中找到machine.config文件,其中有一个<processModel>的tag:

<processModel enable="true" ... userName="machine" password="AutoGenerate" ... />

其中的userName="machine"就是指定使用本机ASPNET账号(.\ASPNET)作为ASP.NET工作进程的默认账号。

这个默认账号是可以修改的,如果开发人员将userName修改为一个域用户账号:
<system.web>
                <processModel enable="true" userName="domain\user" password="password"/>
</system.web>

那么ASP.NET工作进程的身份就变成了domain\user。只要这个域用户账号拥有需要的权限,那么machine.config所在机器上所有ASP.NET程序都可以访问域中其他服务器的资源,包括存取用网络共享路径指定的文件。

如果指定userName="system",那么ASP.NET程序将以LocalSystem身份运行,可以存取几乎所有本地资源,因此非常危险!

无论machine.config指定哪个默认账号,这个默认账号都可以被应用程序的web.config设置的Impersonation覆盖,即特定应用程序可以以其他身份运行。

///////////////////////////////////////////////
在代码中模拟IIS认证账号
在代码中使用身份模拟更加灵活,可以在指定的代码段中使用身份模拟,在该代码段之外恢复使用ASPNET本机账号。该方法要求必须使用Windows的认证身份标识。下面的例子在代码中模拟IIS认证账号:
System.Security.Principal.WindowsImpersonationContext impersonationContext;
impersonationContext = ((System.Security.Principal.WindowsIdentity)User.Identity).Impersonate();

//Insert your code that runs under the security context of the authenticating user here.

impersonationContext.Undo();
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
AD 单点登录以及windows认证详细说明
Troubleshooting ABCpdf and Microsoft Office Issues
SQL Server 2008 R2 SSRS 安装配置后无法使用问题的解决方法
d
MVC5
ASP.NET页面与IIS底层交互和工作原理详解
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服