Euphoria Audio, LLC

Audio, Video and Data Consulting

VB.Net: Remotely Authenticate to Access Network Share

5 Comments »

I highly recommend that you import the NetUseDel function as well. If you try to connect to a share on a remote machine while you are connected to the same remote using another set of credentials, even if it's a different share, Windows won't let you and you'll receive an error. So the best thing to do before attempting a connection is to iterate through all of the connections on your machine (just open a command prompt and type "NET USE" and hit enter to see) and delete any existing connections to the remote machine. There are likely other methods in the dll that you can use for listing connections. Thus to import NetUseDel, use:

''' <summary>
''' Imports the NetUseDel method from the NetAPI32 that tells windows to disconnect a UNC share
''' </summary>
''' <param name="UncServerName">Share name, usually set this to Nothing</param>
''' <param name="UseName">The shared resource on the local computer that should be disconnected. Usually
''' the ui2_remote value from USE_INFO_2</param>
''' <param name="ForceCond">Level of closure to set, i.e. don't close if files are open, etc</param>
''' <returns>o if successful, a system error code if there was an error</returns>
<DllImport("C:\windows\system32\netapi32.dll")> Public Shared Function NetUseDel(ByRef UncServerName As String, ByVal UseName As String, ByVal ForceCond As ForceCond) As UInt32
End Function

And finally, here is a little function that you can use to simplify connections to share. Note that it does not perform any drive mapping, just creates a connection using the provided credentials and then you will have to navigate to a the UNC using some other means.

''' <summary>
''' Attempts to connect to a remote share using the provided credentials. After authenticating, you
''' should be able to accesss the remote share. After you are finished with the share, call NetUseDel
''' </summary>
''' <param name="strUNCPath">UNC path to the share you want to access</param>
''' <param name="strUser">User name to logon with</param>
''' <param name="strDomain">Domain under which to logon. If local machine, use "localhost"</param>
''' <param name="strPass">Password to use</param>
''' <returns>True if the logon and access was successful, false if there was an error</returns>
Public Shared Function NetUseWithCredentials(ByVal strUNCPath As String, ByVal strUser As String, ByVal strDomain As String, ByVal strPass As String) As Boolean
    ' data check
    If (strUNCPath.Length < 2 Or strUser.Length < 2) Then
        Console.WriteLine("Remote_Logon." & (New System.Diagnostics.StackFrame()).GetMethod().Name & " Invalid path or user: " & strUNCPath & " | " & strUser)
        Return False
    End If

    Try
        ' delcar vars
        Dim useinfo As New USE_INFO_2()
        Dim paramErrorIndex As UInt32
        Dim returncode As UInt32

        ' store values in our structure
        useinfo.ui2_remote = strUNCPath
        useinfo.ui2_username = strUser
        useinfo.ui2_domainname = strDomain
        useinfo.ui2_password = strPass
        useinfo.ui2_asg_type = 0
        useinfo.ui2_usecount = 1

        ' try logging in
        returncode = NetUseAdd(Nothing, 2, useinfo, paramErrorIndex)

        ' check return code
        If (returncode = 0) Then
            Return True
        Else
            Console.WriteLine("Remote_Logon." & (New System.Diagnostics.StackFrame()).GetMethod().Name & " Unable to logon: " & strUNCPath & " | " & strUser & " | " & returncode)
            Return False
        End If
    Catch ex As Exception
        Console.WriteLine("Remote_Logon." & (New System.Diagnostics.StackFrame()).GetMethod().Name & " " & ex.ToString())
        Return False
    End Try
End Function
Pages:  1   2 

5 Responses

Good point Daniel, this should probably be used more for a server situation where only admins have the ability to logon, not for desktops.

  • This is good option to get access to network folders, the only disadvantage of this, is that when You open connection for example to \\172.21.7.218\ShareTest , user on the computer in which You did it has also full acces to that folder (not only program he runs), although he shouldn't. This is a bit risky.

  • Fantastic!
    Works great where other methods failed.
     
     

  • Hi Leon, That’s strange as it looks like you’re using it correctly. If you run the NetUseWithCredentials, does it connect properly and can you see the connection from a command line using NET USE? Make sure the name under NET USE is exactly the same as what you put in NetUseDel. I’m guessing it may drop the dollar sign for some reason.

  • Dear Chris,

    Thank you SOOO much for the code and explanation about accessing network shares on non domain servers and passing the username and password to obtain access.  

    I'd been looking for several days for something like this.  I had found C#, C and C++ examples, but am completely illiterate with those languages.

    It took me some time, but finally I got the part on gaining access to the network share working.  The problem at the moment for me is that I get error 2250, network share does not exist, when trying to use NetUseDel to disconnect the share.

    -I know it's much to ask, but might you have some suggestions?

    The code I use calling the function in your class is as follows.:
    NetUseWithCredentials("\\172.21.7.218\ShareTest$", "Username", Nothing, "Password")

    The code using NetUseDel, and giving error 2250, is the following:
    NetUseDel(Nothing, "\\172.21.7.218\ShareTest$", ForceCond.USE_LOTS_OF_FORCE)

    From your comments I understand that this should be correct.

    Using net use from a command window does show the share, and using net use from that same command prompt to remove the share works properly.

    I hope you might have some suggestions.

    Thanks for your kind help and patience.

    Best regards,

    Leon

    droopy928gt@yahoo.com

  • Leave a Reply