使用ASP.NET访问共享文件夹

使用ASP.NET访问共享文件夹,asp.net,authentication,impersonation,shared-directory,Asp.net,Authentication,Impersonation,Shared Directory,我有一个ASP.NET应用程序,我想访问位于另一台服务器上的共享文件夹,我解释了这种情况: 服务器A:OS:Windows Server 2008;网络服务器:IIS7;ASP.NET FRAMEWORK 2.0 服务器B:OS:Linux;文件夹共享(服务用户,密码保护) 我尝试了以下代码: Dim impersonationContext As WindowsImpersonationContext Declare Function LogonUserA Lib "advapi32.d

我有一个ASP.NET应用程序,我想访问位于另一台服务器上的共享文件夹,我解释了这种情况:
服务器A:OS:Windows Server 2008;网络服务器:IIS7;ASP.NET FRAMEWORK 2.0
服务器B:OS:Linux;文件夹共享(服务用户,密码保护) 我尝试了以下代码:

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
    Dim _impersonatedUser As System.Security.Principal.WindowsImpersonationContext

    Const LOGON32_LOGON_NEW_CREDENTIALS As Integer = 9
    Const LOGON32_PROVIDER_WINNT50 As Integer = 3
    Const SecurityImpersonation As Integer = 2

    Dim win32ErrorNumber As Integer
    Dim _tokenHandle As New IntPtr(0)
    Dim _dupeTokenHandle As New IntPtr(0)

    _tokenHandle = IntPtr.Zero
    _dupeTokenHandle = IntPtr.Zero

    If RevertToSelf() Then
        If Not LogonUserA(userName, domain, password, 2, 0, token) Then
            win32ErrorNumber = System.Runtime.InteropServices.Marshal.GetLastWin32Error()
            _Alert("ERROR NUMBER: " + win32ErrorNumber.ToString)
        Else
            If DuplicateToken(token, 2, tokenDuplicate) <> 0 Then
                _Alert("token: " + token.ToString)
                _Alert("token Duplciate: " + tokenDuplicate.ToString)
                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
Dim 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)声明为
私有函数impersonateValidUser(ByVal用户名为字符串_
ByVal域作为字符串,ByVal密码作为字符串)作为布尔值
将tempWindowsIdentity设置为WindowsIdentity
作为IntPtr=IntPtr.Zero的Dim令牌
Dim标记复制为IntPtr=IntPtr.Zero
impersonateValidUser=False
Dim\u模拟用户作为System.Security.Principal.WindowsImpersonationContext
Const LOGON32\u LOGON\u NEW\u凭据为整数=9
Const LOGON32\u PROVIDER\u WINNT50为整数=3
常量SecurityImpersonation为整数=2
将win32ErrorNumber设置为整数
Dim_令牌句柄作为新IntPtr(0)
Dim_dupeTokenHandle作为新IntPtr(0)
_tokenHandle=IntPtr.Zero
_dupeTokenHandle=IntPtr.Zero
如果返回到self(),则
如果不是LogonUserA(用户名、域、密码、2、0、令牌),则
win32ErrorNumber=System.Runtime.InteropServices.Marshal.GetLastWin32Error()
_警报(“错误号:+win32ErrorNumber.ToString”)
其他的
如果DuplicateToken(token,2,tokenDuplicate)0,则
_警报(“令牌:+token.ToString”)
_警报(“令牌重复:”+tokenDuplicate.ToString)
tempWindowsIdentity=新WindowsIdentity(令牌重复)
impersonationContext=tempWindowsIdentity.Impersonate()
如果不是,则impersonationContext什么都不是
impersonateValidUser=True
如果结束
如果结束
如果结束
如果结束
如果不是重复.Equals(IntPtr.Zero),则
CloseHandle(重复标记)
如果结束
如果不是token.Equals(IntPtr.Zero),则
CloseHandle(令牌)
如果结束
端函数
LogonUserA函数始终返回“false”。

如何解决这个问题,基本上我需要登录到serverB文件夹。

我可以立即说的是,上面说“您不能使用LogonUser登录到远程计算机”。这可能是ActiveDirectory/LDAP环境吗?嗨,Alexander,谢谢您的回答。我不确定是否使用ActiveDirectory/LDAP环境,但如果需要,可以询问。无论我使用何种方法,我都需要从代码访问共享文件夹中的某些文件,该文件夹位于另一台服务器上,并受用户名和密码保护。对于使用netapp的共享文件夹,您知道有什么方法可以帮助我解决此问题吗?