Impersonate in order to access a share in .NET

Thursday, 5 April 2007 02:38 by Myself

Today I discovered the best way to access a shared folder across the local network.

This example works outside a domain as well.

I tried to use the web.config directive first, .
But this had too many unwanted effects, when all I wanted to do was upload a file to a shared folder.

So, by using the LogonUser Win32 API I was able to wrap a class around this nicely


22 using (new Impersonator(true, "username", "password", "123.123.123.123"))


23 {


24 if (!Directory.Exists(@"\\123.123.123.123\Foo\bar"))


25 Directory.CreateDirectory(@"\\123.123.123.123\Foo\bar");


26 }


All calls made inside the using clause will then be impersonated.

You might as well use an overloaded constructor to provide the username, password and domain/machine/IP.

Source code: download (Temporarily down) 

Cheers!

Tags:   , , ,
Categories:   Development
Actions:   E-mail | Permalink | Comments (1) | Comment RSSRSS comment feed

Related posts

Comments

January 11. 2008 16:34

Anonymous

Damn dude! Thanks a bunch, you saved ½ days work! Have been trying out other solutions, but this one is by far the finest and best!

Anonymous

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

January 6. 2009 01:23

 
Software