欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

ASP.Net使用System.Security.Principal模擬用戶

 更新時間:2022年05月13日 08:34:50   作者:springsnow  
這篇文章介紹了ASP.Net使用System.Security.Principal模擬用戶的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

一、概述

在實際的項目開發(fā)中,我們可能會需要調用一些非托管程序,而有些非托管程序需要有更高的身份權限才能正確執(zhí)行。本文介紹了如何讓IIS承載的ASP.NET網站以特定的賬戶執(zhí)行,比如Administrator。

默認情況下禁用 ASP.NET 模擬。如果對某 ASP.NET 應用程序啟用了模擬,該應用程序將運行在標識上下文中,其訪問標記被 IIS 傳遞給 ASP.NET。

  • 該標記可以是已通過身份驗證的用戶標記(如已登錄的 Windows 用戶的標記)【IIS上配置“集成Widows身份驗證”且不勾選“匿名訪問”的情況下】
  • 該標記也可以是 IIS 為匿名用戶提供的標記(通常為 IUSR_MACHINENAME 標識)。 【IIS上配置勾選“匿名訪問”的情況下】

二、讀取被模擬用戶的標識

注意:可以使用以下代碼來確定線程作為哪個用戶執(zhí)行:

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();
}

到此這篇關于ASP.Net使用System.Security.Principal模擬用戶的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • asp.net“服務器應用程序不可用” 解決方法

    asp.net“服務器應用程序不可用” 解決方法

    服務器應用程序不可用 您試圖在此 Web 服務器上訪問的 Web 應用程序當前不可用。請點擊 Web 瀏覽器中的“刷新”按鈕重試您的請求。 管理員注意事項: 詳述此特定請求失敗原因的錯誤消息可在 Web 服務器的系統(tǒng)事件日志中找到。請檢查此日志項以查明導致該錯誤發(fā)生的原因。
    2008-10-10
  • IIS處理Asp.net請求和Asp.net頁面生命周期說明

    IIS處理Asp.net請求和Asp.net頁面生命周期說明

    當一個客戶端頁面訪問IIS試圖獲取一些信息的時候,發(fā)生了什么事情?一個請求在通過了HTTP管道后又發(fā)生了什么?本文主要是描述這兩個過程,即IIS處理asp.net請求和asp.net的頁面生命周期。歡迎大家積極拍磚,共同學習,共同進步。
    2011-05-05
  • ASP.NET實現(xiàn)上傳Excel功能

    ASP.NET實現(xiàn)上傳Excel功能

    本文主要介紹了ASP.NET 實現(xiàn)上傳EXCEL,利用NOPI操作,轉換得到DataTable的相關知識。具有很好的參考價值,下面跟著小編一起來看下吧
    2017-03-03
  • .NET6自定義WebAPI過濾器

    .NET6自定義WebAPI過濾器

    這篇文章介紹了.NET6自定義WebAPI過濾器的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-12-12
  • .net實現(xiàn)微信公眾賬號接口開發(fā)實例代碼

    .net實現(xiàn)微信公眾賬號接口開發(fā)實例代碼

    這篇文章主要介紹了.net實現(xiàn)微信公眾賬號接口開發(fā)實例代碼,有需要的朋友可以參考一下
    2013-12-12
  • asp.net中js+jquery添加下拉框值和后臺獲取示例

    asp.net中js+jquery添加下拉框值和后臺獲取示例

    這篇文章主要介紹了asp.net中js+jquery添加下拉框值和后臺獲取的具體實現(xiàn),需要的朋友可以參考下
    2014-05-05
  • IdnentiyServer使用客戶端憑據(jù)訪問API的實例代碼

    IdnentiyServer使用客戶端憑據(jù)訪問API的實例代碼

    這篇文章主要介紹了IdnentiyServer-使用客戶端憑據(jù)訪問API的相關知識,非常不錯,具有一定的參考借鑒價值 ,需要的朋友可以參考下
    2018-10-10
  • C#實現(xiàn)EXCEL數(shù)據(jù)到TXT文檔的轉換

    C#實現(xiàn)EXCEL數(shù)據(jù)到TXT文檔的轉換

    C#實現(xiàn)EXCEL數(shù)據(jù)到TXT文檔的轉換,需要的朋友可以參考一下
    2013-02-02
  • asp.net 文章分頁顯示實現(xiàn)代碼

    asp.net 文章分頁顯示實現(xiàn)代碼

    asp.net 文章分頁顯示實現(xiàn)代碼,不多說看代碼,簡單,自己請適當修改
    2012-06-06
  • ASP.NET表單驗證方法詳解

    ASP.NET表單驗證方法詳解

    在表單提交的時候,經常需要對錄入信息的長度、格式、內容等進行驗證,以便獲得合理的信息。在ASP.NET開發(fā)中主要的驗證方法,我總結了一下,主要有一下幾種,如有不足之處請朋友們予以指出。
    2008-09-09

最新評論