一、概述

在实际的项目开发中,我们可能会需要调用一些非托管程序,而有些非托管程序需要有更高的身份权限才能正确执行。本文介绍了如何让IIS承载的ASP.NET网站以特定的账户执行,比如Administrator。

默认情况下禁用 ASP.NET 模拟。如果对某 ASP.NET 应用程序启用了模拟,该应用程序将运行在标识上下文中,其访问标记被 IIS 传递给 ASP.NET。

  • 该标记可以是已通过身份验证的用户标记(如已登录的 Windows 用户的标记)【IIS上配置“集成Widows身份验证”且不勾选“匿名访问”的情况下】
  • 该标记也可以是 IIS 为匿名用户提供的标记(通常为 IUSR_MACHINENAME 标识)。 【IIS上配置勾选“匿名访问”的情况下】

二、读取被模拟用户的标识

注意:可以使用以下代码来确定线程作为哪个用户执行:

WindowsIdentity.GetCurrent().Name

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

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

<identity impersonate="true" />

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

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

<identity impersonate="true" userName="accountname" password="password" />

五、在代码中模拟身份验证用户

若要仅在运行代码的特定部分时模拟身份验证用户 (User.Identity),您可以使用以下代码。此方法要求身份验证用户标识的类型为 WindowsIdentity

WindowsImpersonationContext impersonationContext =  ((WindowsIdentity)User.Identity).Impersonate();

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

impersonationContext.Undo();

六、在代码中模拟特定用户

若要仅在运行代码的特定部分时模拟特定用户,请使用以下代码(使用Windows API):

<%@ Page Language="C#"%>
<%@ Import Namespace = "System.Web" %>
<%@ Import Namespace = "System.Web.Security" %>
<%@ Import Namespace = "System.Security.Principal" %>
<%@ Import Namespace = "System.Runtime.InteropServices" %> <script runat=server>
public const int LOGON32_LOGON_INTERACTIVE = ;
public const int LOGON32_PROVIDER_DEFAULT = ; WindowsImpersonationContext impersonationContext; [DllImport("advapi32.dll")]
public static extern int LogonUserA(String lpszUserName,
String lpszDomain,
String lpszPassword,
int dwLogonType,
int dwLogonProvider,
ref IntPtr phToken);
[DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
public static extern int DuplicateToken(IntPtr hToken,
int impersonationLevel,
ref IntPtr hNewToken); [DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
public static extern bool RevertToSelf(); [DllImport("kernel32.dll", CharSet=CharSet.Auto)]
public static extern bool CloseHandle(IntPtr handle); public void Page_Load(Object s, EventArgs e)
{
if(impersonateValidUser("username", "domain", "password"))
{
//Insert your code that runs under the security context of a specific user here.
undoImpersonation();
}
else
{
//Your impersonation failed. Therefore, include a fail-safe mechanism here.
}
} private bool impersonateValidUser(String userName, String domain, String password)
{
WindowsIdentity tempWindowsIdentity;
IntPtr token = IntPtr.Zero;
IntPtr tokenDuplicate = IntPtr.Zero; if(RevertToSelf())
{
if(LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE,
LOGON32_PROVIDER_DEFAULT, ref token) != )
{
if(DuplicateToken(token, , ref tokenDuplicate) != )
{
tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
impersonationContext = tempWindowsIdentity.Impersonate();
if (impersonationContext != null)
{
CloseHandle(token);
CloseHandle(tokenDuplicate);
return true;
}
}
}
}
if(token!= IntPtr.Zero)
CloseHandle(token);
if(tokenDuplicate!=IntPtr.Zero)
CloseHandle(tokenDuplicate);
return false;
} private void undoImpersonation()
{
impersonationContext.Undo();
}

ASP.Net模拟用户 System.Security.Principal的更多相关文章

  1. 【windows 访问控制】十二、C#实操 主体 System.Security.Principal 案例

    案例1.主体(包含用户和组)和标识(用户名)的使用. PrincipalPolicy枚举:主体类型 分为window主体.未认证的主体和未分配主体GenericPrincipal.GenericIde ...

  2. 谈 IIS7.5 Asp.Net模拟用户

    IIS  Asp.模拟用户官方的解释是: 如果要在非默认安全上下文中运行 ASP.NET 应用程序,请使用 ASP.NET 模拟身份验证. 如果您对某个 ASP.NET 应用程序启用了模拟,那么该应用 ...

  3. 如何在 ASP.NET 应用程序中实现模拟用户身份(在ASP.NET中以管理员身份运行网站)

    前言 在实际的项目开发中,我们可能会需要调用一些非托管程序,而有些非托管程序需要有更高的身份权限才能正确执行.本文介绍了如何让IIS承载的ASP.NET网站以特定的账户执行,比如Administrat ...

  4. identity与ASP.NET 模拟

    默认情况下,ASP.NET应用程序以本机的ASPNET帐号运行,该帐号属于普通用户组,权限受到一定的限制,以保障ASP.NET应用程序运行的安全.但是有时需要某个ASP.NET应用程序或者程序中的某段 ...

  5. (C#)为应用程式设定运行权限(System.Security类下的GenericIdentity,GenericPrincipal,PrincipalPermission)

    最近看书<编写高质量代码改善C#程序的157个建议>,知识点备忘: System.Security.Principal.GenericIdentity==>表示一般用户 System ...

  6. 【windows 访问控制】十一、C# 实操 对象 System.Security.AccessControl 命名空间

    AccessControl 命名空间 结构图 解说: DirectorySecurity=目录ACLFileSecurity=文件ACLFileSystemAuditRule=目录和文件中SACL中的 ...

  7. WebApi 数据保护操作未成功。这可能是由于未为当前线程的用户上下文加载用户配置文件导致的。当线程执行模拟时,可能会出现此情况。","ExceptionType":"System.Security.Cryptography.CryptographicException","StackTrace

    在调用System.Security.Cryptography.ProtectedData.Protect方法来保护私密信息时,IIS可能会报以下错误:CryptographicException: ...

  8. 部署时,出现用户代码未处理 System.Security.Cryptography.CryptographicException 错误解决方法

    转载:http://www.cnblogs.com/jys509/p/4499978.html 在调用RSA加密的.pfx密钥时,在本地调试没有问题,可以布署到服务器,就会报以下的错误: 用户代码未处 ...

  9. ASP.NET MVC 用户登录Login

    ASP.NET MVC 用户登录Login一.先来看个框架例子:(这个是网上收集到的)  第一步:创建一个类库ClassLibrary831.            第二步:编写一个类实现IHttpM ...

随机推荐

  1. Loaded plugins: fastestmirror, refresh-packagekit, security

    问题描述 最近在用Centos 6.7的时候出现了这种情况 Loaded plugins: fastestmirror, refresh-packagekit, security Loading mi ...

  2. WebRoot/WEBINF下的classes文件内无法生成编译文件,classes下没有文件,eclipse无法编译项目

    其实主要问题还是一个eclipse配置的问题. 如下图.将这一项的勾取消掉. 转自:https://blog.csdn.net/qq_36443497/article/details/79684231

  3. SQL Server直接执行.sql文件

    SQL Server直接执行.sql文件    客户的数据库数据被篡改,利用Log Explorer工具根据日志生成的回滚脚本有200多M,不可能一下子扔到查询分析器里去执行,于是想是否SQL Ser ...

  4. Java基础---Java方法的重载Overload

    对于功能类似的方法来说,因为参数列表不一样,却需要记住那么多不同的方法名称,太麻烦. 方法的重载(Overload):多个方法的名称一样,但是参数列表不一样.好处:只需要记住唯一一个方法名称,就可以实 ...

  5. 【C++札记】类的分离式写法

    介绍 类的分离式写法,使得代码更加规范,增强了阅读性. 分离式写法的规则: 1.类的变量:写在类的里面 2.成员函数:类中写函数的声明,函数的定义写在类体外. 3.写在类外函数定义时,类名前加限定(O ...

  6. 网络编程[第一篇]基于tcp协议的套接字编程

    将服务端-客户端的连接比作双方打电话的过程 2019-07-24 一.客户端 主动的一方: 客户端实例化一个socket对象--> 主动像服务端发送连接请求--> (服务端接受请求后即可进 ...

  7. MySQL 常用函数介绍

    MySQL 基础篇 三范式 MySQL 军规 MySQL 配置 MySQL 用户管理和权限设置 MySQL 常用函数介绍 MySQL 字段类型介绍 MySQL 多列排序 MySQL 行转列 列转行 M ...

  8. js中 this 的指向

    js中 this的指向一共存在3种地方: 1.全局的this; 2.构造函数的this; 3.call/apply; 一.全局的this: function test(){ this.d = 3;// ...

  9. C# 整型数和浮点型数的进制转换

    1.十进制转二进制 /// <summary> /// 十进制转二进制 /// </summary> public class Convert10To2 { /// <s ...

  10. C#强制回收垃圾

    [DllImport("psapi.dll")] private static extern int EmptyWorkingSet(int hProcess); public v ...