星期二, 2月 19, 2008

ASP.NET 如何上傳檔案至遠端機器上

      最近專案遇到一個問題, 需要將檔案存至遠端機器上, 可是權限無法存取遠端機器, Google 了一下, 有幾種解法, 都是需要設定 Web.config, 也就是說當執行 IIS 時就以該使用者去執行, 這樣會衍生出更多不必要的問題, 所以不是我要的方法。

      後來找到一種方法, 是透過呼叫 Win32 API 可定義執行緒使用自訂權限去執行動作, 理論上就可以解決我的問題了, 寫了一支簡單程式測試,   看起來是 ok 的...

      以下為程式碼, 藍色為重點部份...


RemoteCopy.aspx

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="RemoteCopy.aspx.vb" Inherits="RemoteCopy" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>未命名頁面</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        &nbsp;<asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:Button ID="Button1" runat="server" Text="Button" /></div>
    </form>
</body>
</html>


RemoteCopy.aspx.vb

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

Partial Class RemoteCopy
    Inherits System.Web.UI.Page
    Dim LOGON32_LOGON_INTERACTIVE As Integer = 2
    Dim LOGON32_PROVIDER_DEFAULT As Integer = 0
    Dim impersonationContext As WindowsImpersonationContext

    Declare Function LogonUserA Lib "advapi32.dll" (ByVal lpszUsername As String, _
                        ByVal lpszDomain As String, _
                        ByVal lpszPassword As String, _
                        ByVal dwLogonType As Integer, _
                        ByVal dwLogonProvider As Integer, _
                        ByRef phToken As IntPtr) As Integer

    Declare Auto Function DuplicateToken Lib "advapi32.dll" ( _
                        ByVal ExistingTokenHandle As IntPtr, _
                        ByVal ImpersonationLevel As Integer, _
                        ByRef DuplicateTokenHandle As IntPtr) As Integer

    Declare Auto Function RevertToSelf Lib "advapi32.dll" () As Long
    Declare Auto Function CloseHandle Lib "kernel32.dll" (ByVal handle As IntPtr) As Long

    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

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As SystemEventArgs) Handles Button1.Click
       If impersonateValidUser("userName", "xx.xx.xx.xx", "password") Then
            FileUpload1.SaveAs(
\\xx.xx.xx.xx\test\XX.TXT)
        End If

    End Sub
End Class

沒有留言: