2012-01-20 47 views
1

您好,我正嘗試從ASP.NET Intranet Web應用程序訪問網絡文件夾。這是我的代碼ASP.NET嘗試通過UNC訪問網絡文件夾

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. 

我如何才能使其工作進入我的用戶名和密碼?

+0

我剛剛注意到我試圖通過UNC訪問的服務器不在域中。這是一臺samba服務器的linux機器,它不知道我的域名或我的用戶名。當我通過UNC訪問它時,Samba會詢問用戶名和密碼,以便在該特定機器中創建該用戶名和密碼。 – themis

+0

詢問linux管理員。也許他可以加入這個領域。或者,也許他創建了一些特定於您的需求的用戶名/密碼。 –

回答

5

您的Web應用程序正在使用NETWORK SERVICE帳戶運行。
你必須冒充用戶訪問網絡共享。
您可以在web.config中設置看着identity Element (ASP.NET Settings Schema)

+0

但是,如果我需要多個模仿者? – themis

+0

因爲我有不同的用戶名和密碼的服務器位置以讀取文件,有沒有辦法切換模擬器? – themis

+1

看看[DirectoryInfo登錄](http://stackoverflow.com/questions/1232120/c-how-to-logon-to-a-share-when-using-directoryinfo) –

1

謝謝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 

然後在我的CONTROLER類我用它作爲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服務器,不需要模擬器與該服務器位於同一個域中。

很多thanx