• 关于 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


    MACHINEIUSR_MACHINE 
    -

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

    HttpContext 
    WindowsIdentity 
    Thread


    MACHINEASPNET 
    -

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

    HttpContext 
    WindowsIdentity 
    Thread

    用户提供的名称 
    MACHINEIUSR_MACHINE 
    用户提供的名称

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

    HttpContext 
    WindowsIdentity 
    Thread

    用户提供的名称 
    MACHINEASPNET 
    用户提供的名称

    IIS 基本身份验证

    Web.config 设置

    变量位置

    结果标识

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

    HttpContext 
    WindowsIdentity 
    Thread

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

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

    HttpContext 
    WindowsIdentity 
    Thread

    域用户名 
    MACHINEASPNET 
    域用户名

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

    HttpContext 
    WindowsIdentity 
    Thread

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

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

    HttpContext 
    WindowsIdentity 
    Thread

    用户提供的名称 
    MACHINEASPNET 
    用户提供的名称

    IIS 摘要式身份验证

    Web.config 设置

    变量位置

    结果标识

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

    HttpContext 
    WindowsIdentity 
    Thread

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

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

    HttpContext 
    WindowsIdentity 
    Thread

    域用户名 
    MACHINEASPNET 
    域用户名

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

    HttpContext 
    WindowsIdentity 
    Thread

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

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

    HttpContext 
    WindowsIdentity 
    Thread

    用户提供的名称 
    MACHINEASPNET 
    用户提供的名称

    IIS 集成 Windows

    Web.config 设置

    变量位置

    结果标识

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

    HttpContext 
    WindowsIdentity 
    Thread

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

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

    HttpContext 
    WindowsIdentity 
    Thread

    域用户名 
    MACHINEASPNET 
    域用户名

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

    HttpContext 
    WindowsIdentity 
    Thread

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

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

    HttpContext 
    WindowsIdentity 
    Thread

    用户提供的名称 
    MACHINEASPNET 
    用户提供的名称

    以上各表说明了在 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.NETFrameworkVersionConfig目录中找到machine.config文件,其中有一个<processModel>的tag:

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

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

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

    那么ASP.NET工作进程的身份就变成了domainuser。只要这个域用户帐号拥有需要的权限,那么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
  • 相关阅读:
    mssql sqlserver 表增加列后,视图不会自动更新相关列的两种解决方法分享
    未能加载文件或程序集“**********”或它的某一个依赖项。试图加载格式不正确的程序。
    批量删除MSSQL 中主外键约束
    mssql sqlserver xml数据类型专题
    mssql sqlserver for xml EXPLICIT 用法详解说明
    mssql sqlserver null数据类型专题
    Nginx Location模块
    Nginx 服务器的安装部署(CentOS系统)
    Nginx服务器的平滑启动、平缓停止、平滑升级
    CentOS如何配置yum源
  • 原文地址:https://www.cnblogs.com/dragon2017/p/10337679.html
Copyright © 2020-2023  润新知