0

Using .net >= 4, is there a way to access UNC path with authentication (without using Win32 API)? Basically for a given folder path, i want to get its directory listing and load file content.

I found a 6 year old solution with Win32 API Accessing a Shared File (UNC) From a Remote, Non-Trusted Domain With Credentials Is there a modern solution?

Community
  • 1
  • 1
surya
  • 1,351
  • 1
  • 13
  • 29
  • @MethodMan what's with 4.6 - this matched >= 4 ... –  Mar 02 '15 at 20:11
  • http://www.codeproject.com/Articles/43091/Connect-to-a-UNC-Path-with-Credentials try this – MethodMan Mar 02 '15 at 20:13
  • @MethodMan this explicitely mentions Win32 ... [NetUseAdd](https://msdn.microsoft.com/en-us/library/windows/desktop/aa370645.aspx) & [NetUseDel](https://msdn.microsoft.com/en-us/library/windows/desktop/aa370646.aspx) –  Mar 02 '15 at 20:14
  • 1
    Would calling out to a external program ([`net use`](http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/net_use.mspx?mfr=true)) be a acceptable "non native" solution? There is no way to do it in code without using the Win32 API directly or using a library that wraps the Win32 API calls in to managed calls for you (To the best of my knowledge). Also why the restriction to not call Win32 API calls? Are you using something like WinRT where you are not allowed to make native calls? – Scott Chamberlain Mar 02 '15 at 20:16
  • Our company policy is to avoid using Win32 API. – surya Mar 02 '15 at 20:46

1 Answers1

2

The .NET way of doing this is through impersionation. Here are some links to get you started:

How do you do Impersonation in .NET?

http://blogs.msdn.com/b/shawnfa/archive/2005/03/22/400749.aspx

If it is sufficient to impersonate the current user, you can simply use:

using(WindowsIdentity.GetCurrent().Impersonate())
{
  // Within this block you can access the UNC share
} 
Community
  • 1
  • 1
Thomas F.
  • 717
  • 3
  • 13
  • Link only answers are discouraged, please include some of the content in your answer (for example talk about [WindowsImpersonationContext](https://msdn.microsoft.com/en-us/library/system.security.principal.windowsimpersonationcontext(v=vs.110).aspx)). – Scott Chamberlain Mar 02 '15 at 20:25
  • -, still ... wrapping Win32 API under the hood. this is no "non-native" way! And your "ultimate" solutions [refers to Win32 also](https://msdn.microsoft.com/en-us/library/chf6fbt4.aspx) –  Mar 02 '15 at 20:33
  • @ScottChamberlain read the question: *(without using Win32 API)* I know that this makes "no" sense, but this should rather be stated clearly. –  Mar 02 '15 at 20:35
  • 1
    Our company policy is to avoid using Win32 API, i don't mind if Microsoft uses them under the hood, since we don't have to manage it. the network share i want to access has different username and password – surya Mar 02 '15 at 20:38
  • @surya Thanks for your clarification - that makes wrapping a very valid solution! –  Mar 02 '15 at 20:39
  • @AndreasNiedermair can you explain - "that makes wrapping a very valid solution!" – surya Mar 02 '15 at 20:47
  • @surya the .NET framework itself calls Win32API, which equals wrapping - so according to your requirements, this is a valid solution :) –  Mar 03 '15 at 08:27
  • @AndreasNiedermair do you mean for WindowsIdentity.GetCurrent().Impersonate() ? This will work for current user, but the shared directory uses a different user – surya Mar 03 '15 at 13:43
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/72150/discussion-between-surya-and-andreas-niedermair). – surya Mar 03 '15 at 13:47