1

Hello i am trying to access a network folder from my ASP.NET Intranet web application. This is my code

Dim di As DirectoryInfo = New DirectoryInfo("\\10.11.11.172\testfolder")
Dim subFiles() As FileInfo = di.GetFiles()

and i get

Access to the path '\\10.11.11.172\testfolder\' is denied.

How can i enter my username and password in order to make it work?

themhz
  • 8,335
  • 21
  • 84
  • 109
  • I just noticed that the server I am trying to access via UNC is not in the Domain. It is a samba server linux machine that doesn’t know anything about my domain or my username. Samba is asking a username and password when i acces via UNC that it is created in that spesific machine. – themhz Jan 23 '12 at 12:02
  • Ask the linux administrator. Maybe he can join the domain. Or maybe he create some username/password specific for your needs. – Be.St. Jan 23 '12 at 15:35

2 Answers2

5

Your web application is running using NETWORK SERVICE account.
You have to impersonate user to access network share.
You can set it in web.config looking at identity Element (ASP.NET Settings Schema)

Be.St.
  • 4,101
  • 3
  • 25
  • 35
  • But if i need multiple impersonators? – themhz Jan 20 '12 at 13:39
  • Because i have different server locations with different username and passwords in order to read files, is there a way to switch impersonators? – themhz Jan 20 '12 at 13:42
  • 1
    Look at [DirectoryInfo logon](http://stackoverflow.com/questions/1232120/c-how-to-logon-to-a-share-when-using-directoryinfo) – Be.St. Jan 20 '12 at 13:45
  • i cant convert this namespace of classes in vb.net. I dont understand what Name of the user. these params are, should i put them in my web.config? – themhz Jan 20 '12 at 13:53
  • Add a C# class project to your solution. Copy that C# code into new project. Than reference new project from your VB Project and call Impersonate() and UndoImpersonate() methods. Param must be passed as method parameters. – Be.St. Jan 20 '12 at 13:56
  • 1
    The `` parts are part of XML comments. There's an accidental line break between `///` and `` that needs to be removed for those sections, then the code will convert fine. – Chris Haas Jan 20 '12 at 14:17
  • hmmm still tough, The server is sampa linux that I need to access, and the impersonator seems to be using my domain wich I am in when authenticating. Any other work around? – themhz Jan 23 '12 at 11:59
2

Thank you Be.St I convert it to VB.net in order to avoid other users from going into the trouble of doing so. This is the Class that i needed to add to my project

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

then in my controler I use it as Be.St mentions

 <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

This class can be used in order to access files or folders in remote machines via UNC,from asp.net to samba linux server that doesnt require the impersonator to be in the same domain with that server.

Many thanx

themhz
  • 8,335
  • 21
  • 84
  • 109