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();
 
转自:https://blog.csdn.net/goodshot/article/details/7971267

最新文章

  1. GOTO Berlin: Web API设计原则
  2. Android sharePreference使用
  3. 从vs2010的UnitTestFramework类库提取私有方法反射调用的方法
  4. MFC MDI 获取当前视图
  5. [OC Foundation框架 - 16] NSObject和反射
  6. NGUI系列教程二
  7. RecyclerView实现ViewPager效果
  8. SQL数据库知识二(Day 25)
  9. 关于xml的一些操作
  10. vim下单行长文本的时候卡顿解决办法
  11. Java课堂笔记(零):内容索引
  12. 剑指Offer_编程题_21
  13. Centos7 安装系统服务、开机自启动
  14. Redis源码剖析和注释(七)--- 快速列表(quicklist)
  15. ASPxCallbackPanel(珍藏版)
  16. MVC详解(转)
  17. Chrome 浏览器端口的坑:ERR_UNSAFE_PORT
  18. Centos7.1下Docker的安装-yum方法
  19. MongoDB学习--&gt;Spring Data Mongodb--&gt;MongodbTemplate
  20. 在JAVA中封装JSONUtils工具类及使用 (转)

热门文章

  1. C# 将html文本转化为 文本内容方法TextNoHTML
  2. WPF使用样式更新ArcGis InfoWindow外观代码
  3. Exception in thread &quot;main&quot; java.lang.UnsatisfiedLinkError: no awt in java.library.path:
  4. [CentOS]安装软件问题:/lib/ld-linux.so.2: bad ELF interpreter解决
  5. maven(13)-安装nexus私服
  6. CSS3 transition介绍
  7. MySQL案例05:CPU负载优化
  8. 解决WPF图片模糊最佳方法
  9. [翻译] ABCIntroView
  10. Windows事件--重复事件检测