Share via


UNC Path without Domain

Question

Thursday, April 30, 2015 12:23 PM

Hi All,

I have used the following code UNC Path which works well, however if the path is on a machine that is not on a domain but on a workgroup, how can achieve the same outcome with viewing the files?

Regards,

Ben

All replies (13)

Thursday, April 30, 2015 3:38 PM âś…Answered | 1 vote

Domain/workgroup doesn't have any impact on UNC paths.  A UNC path is simply server and share name.  The problem you are more liking having is permissions.  In order to use a share the user must have permissions to the share and NTFS.  For this you generally use a network account but a workgroup computer wouldn't have such an account.  Instead you have to create a local account on the machine and then use those credentials when trying to connect to the path.  But if you're trying to use most of the standard APIs that allow UNC paths then they will always use the current user's credentials.

There are 2 possible solutions that have worked for me.

  1. When the user connects to the remote machine then the remote machine sees the user as a Guest since they cannot be authenticated.  If you open the share and NTFS to allow Guest access then it will work. But this introduces security concerns such that you might not want to do this.
  2. Map the UNC path to a local drive using Win32.  There is no .NET API for this.  As part of the mapping process you can specify the credentials to use.  From that point on it is now a normal drive letter and can be used in standard file calls.  However you need to decide whether the connection is persisted or not and you'll need to remove the mapping if you don't need it anymore.

Michael Taylor
http://blogs.msmvps.com/p3net


Friday, May 1, 2015 9:27 AM

Hi Michael,

Thanks for you reply.

The local machine that is using the above code from code project can access files on the remote server through explorer, this is done by inserting a user that has permissions to the shared folder. I have tried the code on other servers that use a domain controller and it works fine. This makes me believe that the code does need a domain name their and can't be used without one... Does this sound correct?

Regards,

Ben


Friday, May 1, 2015 2:55 PM

If you're connecting to a machine that is on the domain then you'll need to include the domain name as part of the user name. If the machine isn't on the domain then no domain name is needed.


Friday, May 1, 2015 3:54 PM

Which is fine, however the code from the code project example is expecting a domain to enter. Are you saying that this should connect if left blank?

Thanks,

Ben


Friday, May 1, 2015 4:10 PM

I have no idea what code project example you're talking about so I cannot answer that.  In general if you need a domain name then you can use the machine name for a workgroup.  However it really depends upon how the example is putting the user name together.  Ultimately a single user name and a single password will be used.  Domain info is simply concated to that.


Friday, May 1, 2015 5:02 PM

This the code I am referring to: http://www.codeproject.com/Articles/43091/Connect-to-a-UNC-Path-with-Credentials

it is in my first post.

Regards,

Ben


Friday, May 1, 2015 5:36 PM

That code you're referring to is using the older NetUseAdd function.  I would recommend that you instead use the newer API as documented in MSDN.  Besides the fact that it works with the newer resources, it doesn't require the buffer management that the original Net API functions did.

If you want to use the older code anyway then it will set the domain field of the underlying Win32 structure.  MSDN doesn't document the value to use for a workgroup so I would start with an empty or null string and if that doesn't work then use the machine name.


Friday, May 1, 2015 7:59 PM

Do you know of any simuler examples for what I need to achieve using the newer methods? Thanks for you help, Regards, Ben


Friday, May 1, 2015 8:37 PM

I would take a look at Pinvoke.NET which has an example although I haven't verified that it works.


Friday, May 1, 2015 10:04 PM

Thanks, that looks really promising. Do you know if it reads from a path without mapping a drive? Thanks, Ben


Friday, May 1, 2015 11:59 PM

That API will set up a mapped drive as that is what it is designed for.  I'm not aware of a way to connect to a remote share with credentials without using a mapped drive.


Saturday, May 2, 2015 12:43 AM

Do you know if you can drop the mapped drive straight after using it? Regards, Ben


Saturday, May 2, 2015 2:55 AM

You'll want to drop the mapped drive when you're done otherwise you may run into issues trying to add it back if the profile happens to still be loaded. To unmap it you'll use another API function.