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

ASP.NET正在尝试通过UNC访问网络文件夹,asp.net,asp.net-mvc,vb.net,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. 如何输入用户名和密码以使其正常工作 您的w

您好,我正在尝试从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.

如何输入用户名和密码以使其正常工作

您的web应用程序正在使用网络服务帐户运行。
您必须模拟用户才能访问网络共享。

您可以在web.config中设置它,查看您的web应用程序正在使用网络服务帐户运行。
您必须模拟用户才能访问网络共享。
您可以在web.config中设置它,请查看

谢谢您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
公共类用户模拟
Const LOGON32\u LOGON\u INTERACTIVE=2
Const LOGON32\u LOGON\u NETWORK=3
Const LOGON32\u LOGON\u BATCH=4
Const LOGON32\u LOGON\u服务=5
Const LOGON32\u LOGON\u UNLOCK=7
Const LOGON32\u LOGON\u NETWORK\u CLEARTEXT=8
Const LOGON32\u LOGON\u NEW\u凭证=9
Const LOGON32\u提供程序\u默认值=0
Const LOGON32\u PROVIDER\u WINNT35=1
常量LOGON32\u提供程序\u WINNT40=2
Const LOGON32\u提供程序\u WINNT50=3
将impersonationContext设置为WindowsImpersonationContext
声明函数LogonUserA Lib“advapi32.dll”(ByVal lpszUsername作为字符串_
ByVal lpszDomain作为字符串_
ByVal LPSZ密码作为字符串_
ByVal dwLogonType为整数_
ByVal dwLogonProvider作为整数_
ByRef phToken作为IntPtr)作为整数
声明自动函数DuplicateToken库“advapi32.dll”(_
ByVal ExistingTokenHandle作为IntPtr_
ByVal ImpersonationLevel为整数_
ByRef DuplicateTokenHandle(作为IntPtr)作为整数
将自动函数RevertToSelf Lib“advapi32.dll”()声明为
将自动函数CloseHandle Lib“kernel32.dll”(ByVal句柄为IntPtr)声明为
作为布尔值的公共函数impersonateUser(ByVal用户名作为字符串,ByVal域作为字符串,ByVal密码作为字符串)
返回impersonateValidUser(用户名、域、密码)
端函数
公共子用户()
取消模拟()
端接头
私有函数impersonateValidUser(ByVal用户名作为字符串,ByVal域作为字符串,ByVal密码作为字符串)作为布尔值
将tempWindowsIdentity设置为WindowsIdentity
作为IntPtr=IntPtr.Zero的Dim令牌
Dim标记复制为IntPtr=IntPtr.Zero
impersonateValidUser=False
如果返回到self(),则
如果LogonUserA(用户名、域、密码、LOGON32\u登录\u新凭据、LOGON32\u提供程序\u WINNT50、令牌)为0,则
如果DuplicateToken(token,2,tokenDuplicate)0,则
tempWindowsIdentity=新WindowsIdentity(令牌重复)
impersonationContext=tempWindowsIdentity.Impersonate()
如果不是,则impersonationContext什么都不是
impersonateValidUser=True
如果结束
如果结束
如果结束
如果结束
如果不是重复.Equals(IntPtr.Zero),则
CloseHandle(重复标记)
如果结束
如果不是token.Equals(IntPtr.Zero),则
CloseHandle(令牌)
如果结束
端函数
私有子模拟()
impersonationContext.Undo()
端接头
末级
然后在我的控制器中,我把它当作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
_
函数SearchUrlNewDir()作为字符串
Dim impersonateUser作为新用户模拟
impersonateUser.impersonateUser(“用户名”、“密码”)
Dim di As DirectoryInfo=新目录信息(\\10.11.11.172\remfolder)
'Dim subFiles()作为FileInfo=di.GetFiles()
Dim子文件夹()作为DirectoryInfo=di.GetDirectories()
取消模拟用户()
返回“”
端函数
此类可用于通过UNC访问远程计算机中的文件或文件夹,从asp.net到samba linux服务器,这些服务器不要求模拟程序与该服务器位于同一域中

谢谢你,谢谢你 我将其转换为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
公共类用户模拟
Const LOGON32\u LOGON\u INTERACTIVE=2
Const LOGON32\u LOGON\u NETWORK=3
Const LOGON32\u LOGON\u BATCH=4
Const LOGON32\u LOGON\u服务=5
Const LOGON32\u LOGON\u UNLOCK=7
Const LOGON32\u LOGON\u NETWORK\u CLEARTEXT=8
Const LOGON32\u LOGON\u NEW\u凭证=9
Const LOGON32\u提供程序\u默认值=0
Const LOGON32\u PROVIDER\u WINNT35=1
常量LOGON32\u提供程序\u WINNT40=2
Const LOGON32\u提供程序\u WINNT50=3
将impersonationContext设置为WindowsImpersonationContext
声明函数LogonUserA Lib“advapi32.dll”(ByVal lpszUsername作为字符串_
ByVal lpszDomain作为字符串_
ByVal LPSZ密码作为字符串_
ByVal dwLogonType为整数_
ByVal dwLogonProvider作为整数_
ByRef phToken作为IntPtr)作为整数
声明自动函数DuplicateToken库“advapi32.dll”(_
ByVal ExistingTokenHandle作为IntPtr_
ByVal ImpersonationLevel为整数_
ByRef DuplicateTokenHandle(作为IntPtr)作为整数
将自动函数RevertToSelf Lib“advapi32.dll”()声明为
将自动函数CloseHandle Lib“kernel32.dll”(ByVal句柄为IntPtr)声明为
公共函数模拟用户(ByVal用户名为字符串,ByVal域为字符串,ByVal密码为