網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
一、概述
在實(shí)際的項(xiàng)目開(kāi)發(fā)中,我們可能會(huì)需要調(diào)用一些非托管程序,而有些非托管程序需要有更高的身份權(quán)限才能正確執(zhí)行。本文介紹了如何讓IIS承載的ASP.NET網(wǎng)站以特定的賬戶(hù)執(zhí)行,比如Administrator。
默認(rèn)情況下禁用 ASP.NET 模擬。如果對(duì)某 ASP.NET 應(yīng)用程序啟用了模擬,該應(yīng)用程序?qū)⑦\(yùn)行在標(biāo)識(shí)上下文中,其訪(fǎng)問(wèn)標(biāo)記被 IIS 傳遞給 ASP.NET。
- 該標(biāo)記可以是已通過(guò)身份驗(yàn)證的用戶(hù)標(biāo)記(如已登錄的 Windows 用戶(hù)的標(biāo)記)【IIS上配置“集成Widows身份驗(yàn)證”且不勾選“匿名訪(fǎng)問(wèn)”的情況下】
- 該標(biāo)記也可以是 IIS 為匿名用戶(hù)提供的標(biāo)記(通常為 IUSR_MACHINENAME 標(biāo)識(shí))。 【IIS上配置勾選“匿名訪(fǎng)問(wèn)”的情況下】
二、讀取被模擬用戶(hù)的標(biāo)識(shí)
注意:可以使用以下代碼來(lái)確定線(xiàn)程作為哪個(gè)用戶(hù)執(zhí)行:
WindowsIdentity.GetCurrent().Name
三、模擬 IIS 驗(yàn)證的帳戶(hù)或用戶(hù)
若要在收到 ASP.NET 應(yīng)用程序中每個(gè)頁(yè)的每個(gè)請(qǐng)求時(shí)模擬 Microsoft Internet 信息服務(wù) (IIS) 身份驗(yàn)證用戶(hù),必須在此應(yīng)用程序的 Web.config 文件中包含?<identity>?標(biāo)記,并將?impersonate?屬性設(shè)置為?true。例如:
<identity impersonate="true" />
四、為 ASP.NET 應(yīng)用程序的所有請(qǐng)求模擬特定用戶(hù)
若要為 ASP.NET 應(yīng)用程序的所有頁(yè)面上的所有請(qǐng)求模擬特定用戶(hù),可以在該應(yīng)用程序的 Web.config 文件的?<identity>?標(biāo)記中指定?userName?和?password?屬性。例如:
<identity impersonate="true" userName="accountname" password="password" />
五、在代碼中模擬身份驗(yàn)證用戶(hù)
若要僅在運(yùn)行代碼的特定部分時(shí)模擬身份驗(yàn)證用戶(hù) (User.Identity),您可以使用以下代碼。此方法要求身份驗(yàn)證用戶(hù)標(biāo)識(shí)的類(lèi)型為?WindowsIdentity。
WindowsImpersonationContext impersonationContext = ((WindowsIdentity)User.Identity).Impersonate();
//Insert your code that runs under the security context of the authenticating user here.
impersonationContext.Undo();
六、在代碼中模擬特定用戶(hù)
若要僅在運(yùn)行代碼的特定部分時(shí)模擬特定用戶(hù),請(qǐng)使用以下代碼(使用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 = 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();
}
原文鏈接:https://www.cnblogs.com/springsnow/p/9433965.html
相關(guān)推薦
- 2022-11-16 常用的Git便捷操作合集_相關(guān)技巧
- 2022-11-06 Android淺析viewBinding和DataBinding_Android
- 2022-12-15 C#利用KPM算法解決字符串匹配問(wèn)題詳解_C#教程
- 2024-03-10 【Redis】Redis的持久化(備份)
- 2023-06-19 Docker?查詢(xún)、停止、刪除和重啟容器的詳細(xì)過(guò)程_docker
- 2022-11-20 Postgresql刪除數(shù)據(jù)庫(kù)表中重復(fù)數(shù)據(jù)的幾種方法詳解_PostgreSQL
- 2023-02-02 C語(yǔ)言如何實(shí)現(xiàn)三子棋_C 語(yǔ)言
- 2022-02-02 bat命令刪除文件夾
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支