簡體   English   中英

IIS7-使用System.IO.StreamWriter()ASP.net頁進行訪問以拒絕共享訪問

[英]IIS7 - Access Denied using System.IO.StreamWriter() ASP.net page to write to share

我正在處理幾個駐留在一個巨大的Intranet應用程序中的asp.net頁面,該Intranet應用程序池以Identity NetworkService (不要問)身份運行,並使用匿名身份驗證作為IUSR – IIS7。

假設Intranet主目錄是D:\\Intranet\\ -有問題的asp.net頁位於D:\\Intranet\\TimeSheets\\V2\\

我有問題的測試腳本是D:\\Intranet\\TimeSheets\\V2\\Post.aspx ,並在這些行上執行了某些操作(以發布的HTML [以64為基數的字符串形式])-轉換為HTML,然后嘗試寫入網絡共享:

Dim TimeSheetInBase64 As String = Request.Form("HtmlToSave")
Dim HtmlToSave As String = ""
Dim SavePath As String = "\\DifferentFileServer\Public\Random Department\Posted Invoices\"

'#### Convert the HTML from base64 back into HTML
Try
    Dim decodedBytes As Byte()
    decodedBytes = Convert.FromBase64String(TimeSheetInBase64)
    HtmlToSave = Encoding.Default.GetString(decodedBytes)

Catch e As Exception
    echo("Error decoding HTML: " & e.Message)
    Exit Select
End Try

Try
    Dim objWriter As New System.IO.StreamWriter(SavePath & "text.htm", False)
    objWriter.WriteLine(HtmlToSave)
    objWriter.Close()
    echo("OK")

Catch ex As Exception
    Dim wi As WindowsIdentity = System.Security.Principal.WindowsIdentity.GetCurrent()
    Dim user As String = wi.Name
    echo("Error Saving Invoice To Disk (" & wi.Name & "): " & ex.Message)
End Try

嘗試將文件寫入遠程共享時,objWriter引發錯誤:

Error Saving Invoice To Disk (NT AUTHORITY\NETWORK SERVICE): Access to the path '\\DifferentFileServer\Public\Random Department\Posted Invoices\text.htm' is denied.

顯然,這是因為有問題的頁面在應用程序池的范圍內運行。

因此,我嘗試將V2文件夾上的匿名特定用戶更改為使用對相關共享具有寫訪問權限的AD帳戶-但是,即使在保存配置更改后,重新啟動IIS時,該頁面仍然會遇到拒絕訪問錯誤寫入文件(WindowsIdentity.GetCurrent()仍然返回NT AUTHORITY \\ NETWORK SERVICE(這是應用程序池的標識,而不是我為匿名訪問設置的帳戶)。

只是要確認這是覆蓋匿名帳戶的一個問題,我將應用程序池設置為要與我要在匿名特定用戶上使用的AD帳戶一起運行-可以正常工作,並且文件已成功寫入遠程共享-因此憑據很好,只是IIS沒有正確使用它們。

我的問題是,匿名用戶是否可以使用不同的Windows憑據運行某些子文件夾? 如果是這樣,除了更改匿名帳戶似乎無效以外,我還需要做什么?

我的第二個問題是:不是依賴IIS來提升權限,有什么方法可以在asp.net頁內執行此操作,即使用與該頁運行時的憑據不同的憑據寫入文件嗎? 我曾考慮過將這個子文件夾移到它自己的應用程序池中-但這似乎有些混亂,並且我想盡可能避免這樣做。

(對不起,我不知道該怎么辦)

好吧,在我用IIS撞牆之后,我放棄並走了代碼路線,特別是advapi32.dll及其LogonUserA()DuplicateToken()RevertToSelf() ,更重要的是WindowsImpersonationContext對象:

http://msdn.microsoft.com/zh-CN/library/system.security.principal.windowsimpersonationcontext.aspx

首先,聲明函數以啟用或禁用模擬功能:

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

用法非常簡單:

If impersonateValidUser("username", "domain", "password") Then
    '#### Write the file then 'close' the Impersonation
    undoImpersonation()
End If

需要以下命名空間:

System.Web
System.Web.Security
System.Security.Principal
System.Runtime.InteropServices

靈感(可惜我花了很長時間才找到它):

http://support.microsoft.com/kb/306158

暫無
暫無

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

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