ASP.NET尝试通过UNC访问网络文件夹

时间:2012-01-20 13:33:20

标签: asp.net asp.net-mvc vb.net

您好我正在尝试从我的ASP.NET Intranet Web应用程序访问网络文件夹。这是我的代码

Dim di As DirectoryInfo = New DirectoryInfo("\\10.11.11.172\testfolder")
Dim subFiles() As FileInfo = di.GetFiles()

我得到了

Access to the path '\\10.11.11.172\testfolder\' is denied.

如何输入我的用户名和密码才能使其正常工作?

2 个答案:

答案 0 :(得分:5)

您的网络应用程序正在使用NETWORK SERVICE帐户运行 您必须冒充用户访问网络共享 您可以在web.config中设置它,查看identity Element (ASP.NET Settings Schema)

答案 1 :(得分:2)

谢谢Be.St 我将其转换为VB.net,以避免其他用户遇到麻烦。 这是我需要添加到项目中的类

Public Class UserImpersonation

    Const LOGON32_LOGON_INTERACTIVE = 2
    Const LOGON32_LOGON_NETWORK = 3
    Const LOGON32_LOGON_BATCH = 4
    Const LOGON32_LOGON_SERVICE = 5
    Const LOGON32_LOGON_UNLOCK = 7
    Const LOGON32_LOGON_NETWORK_CLEARTEXT = 8
    Const LOGON32_LOGON_NEW_CREDENTIALS = 9
    Const LOGON32_PROVIDER_DEFAULT = 0
    Const LOGON32_PROVIDER_WINNT35 = 1
    Const LOGON32_PROVIDER_WINNT40 = 2
    Const LOGON32_PROVIDER_WINNT50 = 3

    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

    Public Function impersonateUser(ByVal userName As String, ByVal domain As String, ByVal password As String) As Boolean
        Return impersonateValidUser(userName, domain, password)
    End Function

    Public Sub undoimpersonateUser()
        undoImpersonation()
    End Sub

    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_NEW_CREDENTIALS, LOGON32_PROVIDER_WINNT50, 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

End Class

然后在我的控制器中,我将其用作Be.St提及

 <Authorize()> _
        Function SearchUrlNewDir() As String


            Dim impersonateUser As New UserImpersonation
            impersonateUser.impersonateUser("username", "", "password.")

            Dim di As DirectoryInfo = New DirectoryInfo("\\10.11.11.172\remfolder")

            'Dim subFiles() As FileInfo = di.GetFiles()
            Dim subFolders() As DirectoryInfo = di.GetDirectories()

            impersonateUser.undoimpersonateUser()

            Return ""
        End Function

此类可用于通过UNC访问远程计算机中的文件或文件夹,从asp.net到samba linux服务器,不要求模仿者与该服务器位于同一域中。

很多人