2012-01-20 2 views
1

안녕하세요, ASP.NET 인트라넷 웹 응용 프로그램에서 네트워크 폴더에 액세스하려고합니다. 이것은 내 코드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는 UNC를 통해 해당 spesific 컴퓨터에서 생성 될 때 사용자 이름과 암호를 묻습니다. – themis

+0

리눅스 관리자에게 문의하십시오. 어쩌면 그는 도메인에 가입 할 수 있습니다. 또는 사용자의 요구에 맞는 사용자 이름/비밀번호를 만들 수도 있습니다. –

답변

5

웹 응용 프로그램이 NETWORK SERVICE 계정을 사용하여 실행 중입니다.
네트워크 공유에 액세스하려면 사용자를 가장해야합니다.
당신은 내가 그렇게하는 문제에가는 다른 사용자를 방지하기 위해 VB.net로 변환 을 Be.St 감사 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

보고의 Web.config에서 설정할 수 있습니다. 이 내가 내 CONTROLER에

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 서버로의 분장은 해당 서버와 동일한 도메인에 분장자를 필요로하지 않습니다.

많은 thanx

관련 문제