簡體   English   中英

如何通過Microsoft.Web.Administration查找應用程序池的SID?

[英]How can I find the SID for an app pool via Microsoft.Web.Administration?

我正在使用一種配置工具,該工具需要根據特定Web應用程序在其下運行的身份來對目錄設置一些權限。 原始代碼只是基於IIS APPPOOL\\<ApppoolName>或基於眾所周知的SID(如果它是內置帳戶)來構建登錄名。

一些類似的代碼在本地化環境中失敗,因此我現在嘗試擺脫字符串烘焙。

我的解決方案是這樣的:

public static SecurityIdentifier GetApplicationPoolSid(string name)
    {
        ApplicationPool pool = Manager.ApplicationPools[name];
        if (pool != null)
        {
            var sddlForm = pool.GetAttributeValue("applicationPoolSid") as string;
            if (!String.IsNullOrEmpty(sddlForm))
                return new SecurityIdentifier(sddlForm);
        }

        return null;
    }

問題是我通過在調試器中四處查找而找到了"applicationPoolSid" ,而找不到任何文檔說明我沒有利用未記錄的實現功能,該功能將來會消失。 (這意味着它不會通過代碼審查。)

我很想知道我在這里正在做的事情的認可方法。 我也很高興知道IIS APPPOOL\\<ApppoolName>絕對不會被本地化,因此我們可以回到原來的方式。

我找不到對IIS APPPOOL進行本地化的引用,這很有意義,因為IIS和APPPOOL都不是任何語言的單詞,因此沒有任何要翻譯的內容。 盡管它們是英語甚至其他語言的縮寫,但我認為即使在其他語言中,它們仍被稱為IIS和AppPool。

順便說一下,您可以像這樣正式獲得SID

NTAccount f = new NTAccount(@"IIS AppPool\DefaultAppPool");
SecurityIdentifier s = (SecurityIdentifier)f.Translate(typeof(SecurityIdentifier));
String sidString = s.ToString();

如果要將SID傳遞給FileSystemAccessRule() ,則甚至根本不需要SID ,因為它有一個采用IdentityReference的重載,即NTAccount 只需將其傳遞給NTAccount

這將為您提供SID,但不會使用您詢問的功能,而是使用Powershell而不是C#編寫的。 我發布此消息是希望能幫助您找到有關使用SHA1進行生成的正確路徑,而不是實際從Windows查詢。 另外,某些系統未安裝某些SID和用戶帳戶模塊,因此可以在這些情況下使用。

基於apppool用戶名生成SID的功能(底部鏈接中的說明)

function Get-SID ([String]$winver, [String]$username) {

    $sidPrefix = switch ($winver) {
        '6.0' { 'S-1-5-80' }
        default { 'S-1-5-82' }
    }

    $userToString = switch ( $sidPrefix ) {
        'S-1-5-82' { $username.ToLower() }
        default { $username.ToUpper() }
    }

    $userBytes = [Text.Encoding]::Unicode.GetBytes($userToString)
    $sha = New-Object System.Security.Cryptography.SHA1CryptoServiceProvider
    $hash = $sha.ComputeHash($userBytes)

    $sid = $sidPrefix

    for ( $i=0; $i -lt 5; $i++ ) {
        $sid += '-' + [BitConverter]::ToUInt32($hash, $i*4)
    }

    return $sid
}

用法示例

Get-SID -winver "6.0" -username "DefaultAppPool"

Vista或更高版本的用戶6.0,之后請使用6.1 / 6.2等。

盡管有部分功能未包含在代碼中,但該腳本實際上無法正常工作,部分原因是來自winterdom.com的 Tomas Restrepo的強大支持。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM