一、概述

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

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

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

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

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

  1. WindowsIdentity.GetCurrent().Name

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

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

  1. <identity impersonate="true" />

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

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

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

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

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

  1. WindowsImpersonationContext impersonationContext = ((WindowsIdentity)User.Identity).Impersonate();
  2.  
  3. //Insert your code that runs under the security context of the authenticating user here.
  4.  
  5. impersonationContext.Undo();

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

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

  1. <%@ Page Language="C#"%>
  2. <%@ Import Namespace = "System.Web" %>
  3. <%@ Import Namespace = "System.Web.Security" %>
  4. <%@ Import Namespace = "System.Security.Principal" %>
  5. <%@ Import Namespace = "System.Runtime.InteropServices" %>
  6.  
  7. <script runat=server>
  8. public const int LOGON32_LOGON_INTERACTIVE = ;
  9. public const int LOGON32_PROVIDER_DEFAULT = ;
  10.  
  11. WindowsImpersonationContext impersonationContext;
  12.  
  13. [DllImport("advapi32.dll")]
  14. public static extern int LogonUserA(String lpszUserName,
  15. String lpszDomain,
  16. String lpszPassword,
  17. int dwLogonType,
  18. int dwLogonProvider,
  19. ref IntPtr phToken);
  20. [DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
  21. public static extern int DuplicateToken(IntPtr hToken,
  22. int impersonationLevel,
  23. ref IntPtr hNewToken);
  24.  
  25. [DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
  26. public static extern bool RevertToSelf();
  27.  
  28. [DllImport("kernel32.dll", CharSet=CharSet.Auto)]
  29. public static extern bool CloseHandle(IntPtr handle);
  30.  
  31. public void Page_Load(Object s, EventArgs e)
  32. {
  33. if(impersonateValidUser("username", "domain", "password"))
  34. {
  35. //Insert your code that runs under the security context of a specific user here.
  36. undoImpersonation();
  37. }
  38. else
  39. {
  40. //Your impersonation failed. Therefore, include a fail-safe mechanism here.
  41. }
  42. }
  43.  
  44. private bool impersonateValidUser(String userName, String domain, String password)
  45. {
  46. WindowsIdentity tempWindowsIdentity;
  47. IntPtr token = IntPtr.Zero;
  48. IntPtr tokenDuplicate = IntPtr.Zero;
  49.  
  50. if(RevertToSelf())
  51. {
  52. if(LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE,
  53. LOGON32_PROVIDER_DEFAULT, ref token) != )
  54. {
  55. if(DuplicateToken(token, , ref tokenDuplicate) != )
  56. {
  57. tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
  58. impersonationContext = tempWindowsIdentity.Impersonate();
  59. if (impersonationContext != null)
  60. {
  61. CloseHandle(token);
  62. CloseHandle(tokenDuplicate);
  63. return true;
  64. }
  65. }
  66. }
  67. }
  68. if(token!= IntPtr.Zero)
  69. CloseHandle(token);
  70. if(tokenDuplicate!=IntPtr.Zero)
  71. CloseHandle(tokenDuplicate);
  72. return false;
  73. }
  74.  
  75. private void undoImpersonation()
  76. {
  77. impersonationContext.Undo();
  78. }

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. C#基础知识学习 linq 和拉姆表达式二

  2. python3 获取自建gitlab用户提交信息

    #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2019-12-03 14:20 # @Author : Anthony # @Emai ...

  3. consul 初体验

    consul server: 192.168.48.134: #!/bin/bash cd /data/server/consuls nohup /data/server/consuls/consul ...

  4. glib系列2 APP编译

    编译命令 gcc main.c `pkg-config --cflags glib-2.0 --libs glib-2.0` 头文件 $ ls /usr/local/include/glib-2.0/ ...

  5. Python数值日期时间笔记

    数值: 格式化 小数位的处理 随机数: random.choice() 序列中随机选择一个值 random.sample() 获取指定数目的序列 random.shuffle() 打乱顺序 rando ...

  6. asp.net core-9.依赖注入的使用

    http://www.jessetalk.cn/2017/11/06/di-in-aspnetcore/

  7. (转)从0移植uboot (一) _配置分析

    ref : https://www.cnblogs.com/xiaojiang1025/p/6106431.html 本人建议的uboot学习路线,先分析原有配置,根据现有的配置修改.增加有关的部分, ...

  8. Linux学习之如何让普通用户获得ROOT权限

    https://blog.csdn.net/qq_41940950/article/details/81044594

  9. (十五)Hibernate中的多表操作(5):双向多对多

    Hibernate的双向关联. 对象之间可以相互读取.        双向只针对读取的操作.对于增.删除.改的操作没有任何影响. 案例 : 实现双向多对多 MenuBean.java package ...

  10. 移动端 H5 上拉刷新,下拉加载

    http://www.mescroll.com/api.html#options 这里有几个重要的设置 1.down 下不启用下拉刷新是因为再手机端有默认的下拉刷新,会冲突,待解决 2.up 中的 a ...