关于 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();
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值