Click here to Skip to main content
15,895,667 members
Articles / Desktop Programming / Win32

Connect to a UNC Path with Credentials

Rate me:
Please Sign up or sign in to vote.
4.83/5 (47 votes)
16 Oct 2009CPOL2 min read 940.3K   23.4K   95   51
Use NetApi32 to establish and break connections to UNC paths using specified user credentials.

Connection.jpg

Introduction

This article demonstrates how you can connect to a remote resource via a UNC path and pass user credentials. It implements the IDisposable interface so you can use it within a using() block.

Background

I had an ASP.NET site where I wanted to access network resources, but did not have sufficient share permissions because the code ran under the ASP user. I also had a service that copied files every night between file shares in two different domains. I wanted a way to access remote resources without opening up security holes by changing permissions or running as a privileged user.

Using the code

The UNCAccessWithCredentials class implements the Win32 methods NetUseAdd() and NetUseDel() to create the remote connections. Connections added with NetUseAdd are not visible in Explorer. When active, they will show via the DOS Net Use command.

The class also implements the IDisposable interface so that we can use a using() block. Once the end of the block is reached, the class automatically disconnects the UNC connection in its Dispose() methods.

C#
using (UNCAccessWithCredentials unc = new UNCAccessWithCredentials())
{
    if (unc.NetUseWithCredentials(uncpath, user, domain, password))
    {
        // Insert your code that requires access to the UNC resource
    }
    else
    {
        // The connection has failed. Use the LastError to get the system error code
        MessageBox.Show("Failed to connect to " + tbUNCPath.Text + 
                        "\r\nLastError = " + unc.LastError.ToString(),
                        "Failed to connect",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Error);
    }
// When it reaches the end of the using block, the class deletes the connection.
}

If you require persistent connections, declare an instance of the class and use the NetUseWithCredentials() method to connect. Once connected, you can access the remote resource at need until disconnected. Do not forget to remove the connection when you are finished using the NetUseDelete() method..

Interpreting Errors

Error.jpg

If the methods fail, they return false. If this occurs, use the class' LastError property to obtain the Windows System Error Code. You can use the MSDN System Error Codes page to obtain a description of the error.

In the error above, Error 53 is ERROR_BAD_NETPATH - "The network path was not found". It looks like I specified the wrong server or share path.

Points of Interest

In researching how to do this task, I found many interesting hacks that got the job done but were not savory. Some used the LogonUser() method, which only works if your remote user has logon rights on the executing machine. I also found cases where the programmer ran a shell net use command.

Using the API NetUseAdd method, we can control the process within code, without having to hack permissions or resort to DOS commands.

I could not find a way to do this in managed code. If you know of one, please post your solution. I would love to see it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Systems Engineer ThomsonReuters Tax & Accounting
United States United States
I am a Senior System Administrator for a 400+ server ASP farm. With such a large farm and limited staff, our goal is to add as much automation as possible to the system. Most of my programming consists of intelligent slack: spending 2 hours to write a program that handles a reoccurring 10 minute manual job.

Comments and Discussions

 
GeneralBrother thank you very much! Pin
ClaudioTech7-Dec-10 7:27
ClaudioTech7-Dec-10 7:27 
GeneralMy vote of 5 Pin
fnoujaim7-Oct-10 6:41
fnoujaim7-Oct-10 6:41 
Generaldisconnect using NetUseDelete() method Pin
11202002yme*27-Apr-10 6:14
11202002yme*27-Apr-10 6:14 
Generallocal access Pin
Member 96698716-Nov-09 4:18
Member 96698716-Nov-09 4:18 
GeneralLooks good Pin
flipdoubt20-Oct-09 5:22
flipdoubt20-Oct-09 5:22 
GeneralRe: Looks good Pin
hayes.adrian21-Oct-09 11:39
hayes.adrian21-Oct-09 11:39 
GeneralRe: Looks good Pin
flipdoubt22-Oct-09 1:41
flipdoubt22-Oct-09 1:41 
GeneralRe: Looks good Pin
hayes.adrian22-Oct-09 8:14
hayes.adrian22-Oct-09 8:14 
From the link you posted, it looks like your server was Novell. Are you still on a Novell network? If so, I would make sure you're on the latest version of Client32. We've seen tons of problems with flaky Client32 versions - mostly file locking issues. Consider turning off opportunistic locking on the Novell server. If you have OppLocks enabled, two clients may be trying to write to the same file that one of them has locked. It may incur a small performance hit but might just solve the issue.

If you have a Windows server, run perfmon.exe. Right-click on the Counter Logs section and choose New Logs Settings. Give the log a name. Add the following counters: Processor > % Processor Time, Memory > Pages/Sec, and Physical Disk > Avg. Disk Queue Length.

You can review the logs by going to System Monitor and clicking on View Log Data (the disk stack icon on the top bar).

Processor usage is easy. Pages/Sec measures how many times the server has to swap memory out to the page file on disk (extremely slow). It is an indicator of if you have enough RAM. Avg. Disk Queue Length shows the average of how much data is in cache waiting to be written to disk. If this number is above 2 then your disk isn't keeping up with the load the server is placing on it. Consider offloading services to another server or implementing a RAID array.

If your application will only run when the user is logged and runs as the user (not a service running as the Service account), use a mapped drive to connect. This will keep a single session open to the server and cache the connection information. If you are running as a service account or running when the user is not logged in, use the method from this article to run as the user and close the connection.
GeneralRe: Looks good Pin
hayes.adrian21-Oct-09 11:51
hayes.adrian21-Oct-09 11:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.