2012-01-20 8 views
1

こんにちは、私はASP.NETイントラネット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経由でアクセスしようとしているサーバーがドメインにないことに気付きました。私のドメインや私のユーザー名について何も知らないサンバサーバーのLinuxマシンです。 SambaはUNC経由でアクセスする際に、その特定のマシンで作成されたユーザー名とパスワードを要求しています。 – themis

+0

Linux管理者に問い合わせてください。たぶん彼はドメインに加わることができます。または、彼はあなたのニーズに合ったユーザー名/パスワードを作成するかもしれません。 –

答えて

5

ウェブアプリケーションがNETWORK SERVICEアカウントを使用して実行されています。
ネットワーク共有にアクセスするには、ユーザーを偽装する必要があります。
あなたが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ファイルでそれを設定することができますが、私はそうすることのトラブルに行くから、他のユーザーを避けるためにVB.netに変換Be.St ありがとうございます。 この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 

に言及して、私は私のプロジェクトに

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に、私はこのクラスは、ファイルにアクセスするために使用することができ、それを使用していますか、 UNC経由のリモートマシンのフォルダ、asp.netからsamba linuxサーバーへの偽装者は、そのサーバーと同じドメインに存在する必要はありません。

多数あり

関連する問題