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

关于 web.config impersonate 帐号模拟的更多相关文章

  1. Asp.net Web.Config - 配置元素customErrors

    Asp.net配置文件的配置方式,其实在MSDN里面是写得最清楚的了.可惜之前一直未曾了解到MSDN的强大. 先贴个地址:http://msdn.microsoft.com/zh-cn/library ...

  2. 配置Web.config 元素CustomErrors

    一.customErrors 元素 属性 说明 defaultRedirect 指定出错时将浏览器定向到的默认 URL.如果未指定该属性,则显示一般性错误. 可选的属性. URL 可以是绝对的(如 w ...

  3. web.config添加identity impersonate="true"导致拒绝访问

    例:<identity impersonate="tr" userName="AD\name" password="word"/> ...

  4. PCB 工程系统 模拟windows域帐号登入

    一.需求描述: 对于PCB制造企业来说,基本都采用建立共享目享+域名管控权限,好像别的大多数行业都是这样的吧.呵呵 在实际应用中,经常会有这样的问题,自己登入的帐号没有共享目录的权限,但又想通过程序实 ...

  5. PCB 模拟Windows管理员域帐号安装软件

    在我们PCB行业中,局域网的电脑一般都会加入域控的,这样可以方便集中管理用户权限,并可以对访问网络资源可以进行权限限制等. 由于加入了域控对帐号权限的管理,这样一来很多人都无权限安装软件,比如:PCB ...

  6. 用firefox 31配合KeePass密码管理器实现web帐号密码自动填写登录

    原文:http://bbs.kafan.cn/thread-1754676-1-1.html KeePass的优势:1.这是一款完全开源的密码管理器2.很多人都使用lastpass来保存密码,而这种严 ...

  7. Shiro+SpringMVC 实现更安全的登录(加密匹配&登录失败超次数锁定帐号)

    原文:http://blog.csdn.net/wlwlwlwl015/article/details/48518003 前言 初学shiro,shiro提供了一系列安全相关的解决方案,根据官方的介绍 ...

  8. MVC解决方案发布IIS 登录页面需要输入两次帐号问题

    IIS项目在本地VS2013 解决方案中正常登录可以进入.发布IIS时出现需要输入两次帐号密码进入主页面最终发现是web.config文件配置问题 web.config 默认配置 <authen ...

  9. web.config中sessionState节点的配置方案

    web.config中sessionState节点的配置方案 web.config关于sessionState节点的配置方案,sessionState有五种模式:Custom,off,inProc,S ...

随机推荐

  1. Ubuntu 安装 PhpMyAdmin 图文教程

    Ubuntu 安装 PhpMyAdmin 管理 MySQL 数据库 PhpMyAdmin 是一个用 PHP 编写的软件工具,可以通过 web方式控制和操作 MySQL 数据库.通过 phpMyAdmi ...

  2. 在 ubuntu 上运行 php 脚本

    在 ubuntu 上运行 php 脚本 一.配置运行环境 1.要在 ubuntu 上运行 php 脚本,需要安装 Apache2 和 PHP 具体步骤,请参考:Ubuntu 搭建Web服务器(MySQ ...

  3. 131.006 Unsupervised Learning - Feature Scaling | 非监督学习 - 特征缩放

    @(131 - Machine Learning | 机器学习) 1 Feature Scaling transforms features to have range [0,1] according ...

  4. 对WebSocket技术的学习与探索(一)

    WebSocket 简要介绍 WebSocket protocol 是HTML5一种新的协议. 它实现了浏览器与服务器全双工通信(full-duple). 一开始的握手需要借助HTTP请求完成. We ...

  5. [转]Apache的CRT格式SSL证书转换成IIS用的PFX格式

    转自:http://www.getvm.net/apache-crt-ssl-convert-to-iis-pfx/ Apache使用的SSL证书是.crt格式,如果你的网站从Apache换到了win ...

  6. CentOS7 安装 JIRA 7.2.x 教程:下载、安装、汉化、破解

    1.先看视频,参考着能装出个试用版来,不同的地方后面再做说明.JIRA 安装视频(Linux) http://www.confluence.cn/pages/viewpage.action?pageI ...

  7. SqlServer数据库(可疑)的解决办法

    当数据库发生这种操作故障时,可以按如下操作步骤可解决此方法, 打开数据库里的Sql 查询编辑器窗口,运行以下的命令. // 1.使用指定值强制重新配置:(1.0表示为真假) sp_configure ...

  8. 实用的JS正则表达式(手机号码/IP正则/邮编正则/电话等)

    //校验是否全由数字组成 function isDigit(s) { var patrn=/^[0-9]{1,20}$/; if (!patrn.exec(s)) return false retur ...

  9. [翻译] JNWSpringAnimation

    JNWSpringAnimation https://github.com/jwilling/JNWSpringAnimation JNWSpringAnimation is a subclass o ...

  10. - (void)addConstraints:(NSArray<__kindof NSLayoutConstraint *> *)constraints

    Adds multiple constraints on the layout of the receiving view or its subviews.   All constraints mus ...