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

news/2024/5/20 14:21:39/文章来源:https://blog.csdn.net/liuxiaofeng1991/article/details/108443627

前言

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

概要

本文介绍了在 ASP.NET 应用程序中实现模拟用户身份的不同方式。

更多信息

如果要在 ASP.NET 中的线程上模拟用户,可以根据您的要求使用以下方法之一:

  • 模拟 IIS 验证的帐户或用户
  • 为 ASP.NET 应用程序的所有请求模拟特定用户
  • 在代码中模拟身份验证用户
  • 在代码中模拟特定用户

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

System.Security.Principal.WindowsIdentity.GetCurrent().Name

 模拟 IIS 验证的帐户或用户

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

<identity impersonate="true" />

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

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

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

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

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

Visual Basic .NET

复制代码
Dim impersonationContext As System.Security.Principal.WindowsImpersonationContext
Dim currentWindowsIdentity As System.Security.Principal.WindowsIdentity
currentWindowsIdentity = CType(User.Identity, System.Security.Principal.WindowsIdentity)
impersonationContext = currentWindowsIdentity.Impersonate()
'Insert your code that runs under the security context of the authenticating user here.
impersonationContext.Undo()
复制代码

Visual C# .NET

复制代码
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();
复制代码

Visual J# .NET

复制代码
System.Security.Principal.WindowsImpersonationContext impersonationContext;
impersonationContext = 
((System.Security.Principal.WindowsIdentity)get_User().get_Identity()).Impersonate();
//Insert your code that runs under the security context of the authenticating user here.
impersonationContext.Undo();

复制代码

在代码中模拟特定用户

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

复制代码
<%@ Page Language="VB" %>
<%@ Import Namespace = "System.Web" %>
<%@ Import Namespace = "System.Web.Security" %>
<%@ Import Namespace = "System.Security.Principal" %>
<%@ Import Namespace = "System.Runtime.InteropServices" %>
<script runat=server>
Dim LOGON32_LOGON_INTERACTIVE As Integer = 2
Dim LOGON32_PROVIDER_DEFAULT As Integer = 0
Dim impersonationContext As WindowsImpersonationContext
Declare Function LogonUserA Lib "advapi32.dll" (ByVal lpszUsername As String, _
ByVal lpszDomain As String, _
ByVal lpszPassword As String, _
ByVal dwLogonType As Integer, _
ByVal dwLogonProvider As Integer, _
ByRef phToken As IntPtr) As Integer
Declare Auto Function DuplicateToken Lib "advapi32.dll" ( _
ByVal ExistingTokenHandle As IntPtr, _
ByVal ImpersonationLevel As Integer, _
ByRef DuplicateTokenHandle As IntPtr) As Integer
Declare Auto Function RevertToSelf Lib "advapi32.dll" () As Long
Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal handle As IntPtr) As Long
Public Sub Page_Load(ByVal s As Object, ByVal e As EventArgs)
If impersonateValidUser("username", "domain", "password") Then
'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.
End If
End Sub
Private Function impersonateValidUser(ByVal userName As String, _
ByVal domain As String, ByVal password As String) As Boolean
Dim tempWindowsIdentity As WindowsIdentity
Dim token As IntPtr = IntPtr.Zero
Dim tokenDuplicate As IntPtr = IntPtr.Zero
impersonateValidUser = False
If RevertToSelf() Then
If LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE, 
LOGON32_PROVIDER_DEFAULT, token) <> 0 Then
If DuplicateToken(token, 2, tokenDuplicate) <> 0 Then
tempWindowsIdentity = New WindowsIdentity(tokenDuplicate)
impersonationContext = tempWindowsIdentity.Impersonate()
If Not impersonationContext Is Nothing Then
impersonateValidUser = True
End If
End If
End If
End If
If Not tokenDuplicate.Equals(IntPtr.Zero) Then
CloseHandle(tokenDuplicate)
End If
If Not token.Equals(IntPtr.Zero) Then
CloseHandle(token)
End If
End Function
Private Sub undoImpersonation()
impersonationContext.Undo()
End Sub
</script>
复制代码
复制代码
<%@ 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 = 2;
public const int LOGON32_PROVIDER_DEFAULT = 0;
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) != 0)
{
if(DuplicateToken(token, 2, ref tokenDuplicate) != 0) 
{
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();
}
</script>
复制代码
复制代码
<%@ Page language="VJ#" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Web.Security" %>
<%@ Import Namespace="System.Security.Principal" %>
<%@ Import Namespace="System.Runtime.InteropServices" %>
<script runat=server>
public static int LOGON32_LOGON_INTERACTIVE = 2;
public static int LOGON32_PROVIDER_DEFAULT = 0;
WindowsImpersonationContext impersonationContext; 
/** @attribute DllImport("advapi32.dll") */ 
public static native int LogonUserA(String lpszUserName, 
String lpszDomain, 
String lpszPassword,
int dwLogonType, 
int dwLogonProvider, 
System.IntPtr[] phToken);
/** @attribute DllImport("advapi32.dll",
CharSet=CharSet.Auto, SetLastError=true) */ 
public static native int DuplicateToken(System.IntPtr hToken,
int impersonationLevel,
System.IntPtr[] hNewToken);
/** @attribute DllImport("kernel32.dll",CharSet=CharSet.Auto) */ 
public static native  boolean CloseHandle(System.IntPtr[] handle);
/** @attribute DllImport("advapi32.dll",
CharSet=CharSet.Auto,SetLastError=true) */     
public static native boolean RevertToSelf();
public void Page_Load(Object s, System.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 boolean impersonateValidUser(String userName, String domain, String password)
{
WindowsIdentity tempWindowsIdentity;
System.IntPtr[] token = new System.IntPtr[1];
System.IntPtr[] tokenDuplicate = new System.IntPtr[1];
if(RevertToSelf())
{
if(LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE, 
LOGON32_PROVIDER_DEFAULT, token) != 0)
{
if(DuplicateToken(token[0], 2, tokenDuplicate) != 0) 
{
tempWindowsIdentity = new WindowsIdentity(tokenDuplicate[0]);
impersonationContext = tempWindowsIdentity.Impersonate();
if (impersonationContext != null)
{
CloseHandle(tokenDuplicate);
CloseHandle(token);
return true;
}                
}            
} 
}
if(!token[0].Equals(System.IntPtr.Zero))
CloseHandle(token);
if(!tokenDuplicate[0].Equals(System.IntPtr.Zero))
CloseHandle(tokenDuplicate);
return false;
}
private void undoImpersonation()
{
impersonationContext.Undo();
}
</script>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.luyixian.cn/news_show_854585.aspx

如若内容造成侵权/违法违规/事实不符,请联系dt猫网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

今日头条一个专门做“特卖”的网站

特色推荐喜好推荐不知道大家还记得不做的最早的那家无觅&#xff0c;在一点资讯&#xff0c;今日头条还没出来前&#xff0c;大多数站长都有装过他的&#xff0c;相关内容插件和APP&#xff0c;无觅可谓无不强大&#xff0c;网站评论插件&#xff0c;站长内容共享收益&#xff…

天猫淘宝SEO:直通车霸占搜索结果前十页前三结果

有网友微博爆料&#xff1a;天猫淘宝直通车搜索调整&#xff0c;目前在淘宝天猫搜索时&#xff0c;前十页每一页的前三个位置展示结果都是直通车商家。 微博“吴蚊米”爆料&#xff1a;“天猫淘宝的卖家注意了&#xff0c;第二页起的前三个位置都是直通车位了 &#xff0c;第三…

会不会网站建设,要看有没有网站建设技能认证证书了

阿里云大学已经上线很久&#xff0c;关注也很久了&#xff0c;为服务器建设方面提供各种解决方案。阿里云开发者课程、在线实验 、在线考证等多个板块。让开发者从学习到运用到考证让人才得到全方位的发展。 目前阿里云大学考试认证分为三个等级&#xff1a;助理工程师&#xf…

360网站卫士推出风行计划 号称速度提升5倍

申请体验地址&#xff1a;http://wangzhan.360.cn/activity/fengxing/index.php 作者&#xff1a;卢松松&#xff0c;转载请保留出处&#xff01; 转载于:https://my.oschina.net/wangchenyu/blog/1530536

45个“乘法口诀类”域名大揭底:13个.com启用建站

图&#xff1a;45个乘法口决.com域名使用情况 乘法口诀表域名具有易记、顺口等特点&#xff0c;深受国内外投资者的喜爱。从上图数据来看&#xff0c;3数字域名111.com已建国外网站;122.com则建交通网;155.com目前作为迅雷旗下网址导航平台;其中166.com已跳转至网易163.com&…

WordPress网站建设主题模板基本函数整理

WP模板文件 style.css : CSS(样式表)文件index.php : 主页模板archive.php : 文章归档页模板&#xff08;非必需&#xff09;category.php :分类页面模板404.php : Not Found 错误页模板comments.php : 评论模板footer.php : Footer模板header.php : Header模板sidebar.php : 侧…

会不会网站建设,要看有没有网站建设技能认证证书了

阿里云大学已经上线很久&#xff0c;关注也很久了&#xff0c;为服务器建设方面提供各种解决方案。阿里云开发者课程、在线实验 、在线考证等多个板块。让开发者从学习到运用到考证让人才得到全方位的发展。 目前阿里云大学考试认证分为三个等级&#xff1a;助理工程师&#xf…

企业网站建设,如何选择精准域名

如何选择域名&#xff1f; 选择域名应该遵循什么样的原则呢 越短越好&#xff1a;4-6位为最佳。容易记忆后缀选择&#xff1a;优先考虑.COM&#xff08;大家都知道&#xff09;&#xff0c;其次.NET&#xff08;网络&#xff09;&#xff0c;再者.CN&#xff08;中国&#xff0…

教大家怎么开着网站不关站备案

1/所需工具: DNSPod (不知道的话自己百度一下注册个账户然后使用) 网站程序要有定时发布功能/ wordpress程序自带就有,其他程序可以用插件实现 2/现在开始: 网站域名解析的程序一般都是: 域名NS-DNS-A记录/CNAME记录 然后就可以访问的 Dnspod是中国最大的解析商&#xff0c;腾…

政府网站建设,国办发文来规范!《政府网站发展指引》解读

网站开设 政府网站分为政府门户网站和部门网站。县级以上各级人民政府及其部门原则上一个单位最多开设一个网站。 域名规范 政府网站要使用以.gov.cn为后缀的英文域名和符合要求的中文域名&#xff0c;不得使用其他后缀的英文域名。中央人民政府门户网站使用“www.gov.cn”域名…

今天你的wordpress网站被黑了吗?

Hacked By White HAt Hacker 标题跟内容都被改了 貌似4.71版本出了之后没几天就出了4.72真是赶不上速度&#xff0c;自己也比较懒所以就没有操作&#xff0c;因为wordpress最近更新速度确实是太频繁了&#xff0c;或许是漏洞太多了&#xff0c;最近一直在考虑要换zblog还是因…

网站建设域名注册,为什么要选择.COM/.net/.cn

全球域名总量达3.36亿 截至2017年3月31日&#xff0c;全球域名注册总量达到3.36亿&#xff0c;新增域名注册量130万&#xff0c;环比增长0.4%&#xff0c;同比2016年第一季度&#xff0c;增长了3.7%。 点评&#xff1a;全球域名市场依然保持稳定增长。 2、 .com域名总量达1.284…

建设盈利的地方社区网站新手需知

一&#xff1a;地方社区网站的定位和主题的确定 首先我们要知道&#xff0c;我们做地方社区网站要给自己的网站一个什么定位。地方社区网站必须要抓住“地方”两字&#xff0c;了解本地特色&#xff0c;清楚本地行情以及用户的喜好等&#xff0c;总之服务当地用户是我们做地方社…

西安网站建设中小企业到底怎么做网站

针对这样的现象有部分企业老板会选择去培训自己的网络知识&#xff0c;也有一部分企业老板会去招聘一个懂网络的来给他管理网络。对于培训中小企业老板完全没有必要&#xff0c;在做网站的整个过程只要你自己心知肚明就不会被骗&#xff0c;培训的课程往往不太适合&#xff0c;…

网站建设服务器1M带宽支持多少人同时访问?

它们之间的换算关系是1字节(Byte)&#xff1d;8bit&#xff0c;1KB1024B&#xff0c;1MB1024KB&#xff0c;1GB1024MB&#xff0c;1TB1024GB。 1Mbps&#xff08;1024KB&#xff09;带宽&#xff0c;换算到我们熟悉的文件大小&#xff0c;除以8&#xff1b;也就是说1Mbps带宽&a…

网站建设错误代码404/502/509各是什么意思

404和HTTP状态码 404代码的意思是输入错误&#xff0c;找不到要查询的页面。也有可能是网页被删除了。设计404页面的主要原因主要是提醒用户网站可以打开但是页面找不到。尽管404页面被用户浏览到的概率相对于全站的其他页面来说要小得多&#xff0c;但页面难免会出错&#xff…

网站建设域名投资,域名注册切莫盲目

在各大域名商的极力吹捧下&#xff0c;新型域名后缀从一开始上线就注册火爆&#xff0c;有些甚至到了几十万几百万的注册量&#xff0c;想当初.wang域名刚刚出世&#xff0c;就吸引了众多国内站长的眼球&#xff0c;在抢注期更是佳绩不断。双拼&#xff0c;3位的4位的数字字母&…

网站建设网页设计:Web尺寸规范

主流浏览器的界面参数与份额&#xff1a; 系统分辨率统计&#xff1a; >> 查看最新分辨率使用情况 网页宽度与首屏高度&#xff1a; 转载于:https://my.oschina.net/wangchenyu/blog/1530587

什么是网站维护,做好网站维护需要具备哪些知识

网站维护涉及到网站策划&#xff0c;设计&#xff0c;数据分析&#xff0c;运营&#xff0c;优化&#xff0c;推广等方面知识衔接而至。目前中小企业对互联网认识愈发重视&#xff0c;都想在互联网有所发展&#xff0c;于是网站在互联网不只是一个网站。而是更多能带来直接利益…

7条经典传统定律指导网站运营

我一直提倡一个观点&#xff0c;在互联网圈子里面的朋友不要只关注互联网这一个点&#xff0c;或是只关注一些所谓的互联网营销技巧&#xff0c;凡事和互联网扯不上关系的文章或是技法都直接略过或是不加重视&#xff0c;这是一个误区。事实上很多互联网营销技巧是一门拔地而起…