網站首頁 編程語言 正文
一、概述
在實際的項目開發中,我們可能會需要調用一些非托管程序,而有些非托管程序需要有更高的身份權限才能正確執行。本文介紹了如何讓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>?標記中指定?userName?和?password?屬性。例如:
<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 = 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
相關推薦
- 2022-07-30 react組件的生命周期
- 2022-08-20 Python中range()與np.arange()的具體使用_python
- 2023-02-06 C#實現對文件進行加密保護的示例代碼_C#教程
- 2023-01-20 .Net執行SQL存儲過程之易用輕量工具詳解_ASP.NET
- 2022-06-28 C++哈希表之線性探測法實現詳解_C 語言
- 2022-04-17 python數據庫操作之sqlalchemy逆向工程
- 2022-09-04 Python基礎之字典的詳細使用教程_python
- 2022-02-12 前后端的身份認證--cookie--session--jwt--token
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支