Groups | Blog | Home
all groups > dotnet security > june 2005 >

dotnet security : Impersonation in Windows 2000/NT.


Senthamarai
6/20/2005 2:11:03 PM
My program needs to access different network shares from different computers.
One hidden user has permission to the network shared. Whenever the
application needs to access any data from network shared, it has to
impersonate the hidden user and then rever it back to the logged in user.

I found few sample codes using LogonUser, but they don't work in Windows
2000 or NT machines. I'm ready to add/remove any security/policy informaiton
for the network shares, but I don't want to change any policy settings in the
client computer.

Any solution to this problem (in Windows 2k/NT)?

BLiTZWiNG
6/20/2005 11:41:04 PM
I recently went through this, and even though the concept is not simple, the
resulting code is, and doesn't fully use .NET, but uses LogonUser instead.

Firstly, declare some external functions in your class namespace:

namespace a
{
public class b
{
[DllImport("advapi32.DLL", SetLastError = true)]
public static extern int LogonUser(string lpszUsername, string
lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, out
IntPtr phToken);

[DllImport("advapi32.DLL")]
public static extern bool ImpersonateLoggedOnUser(IntPtr hToken); //
handle to token for logged-on user

[DllImport("advapi32.DLL")]
public static extern bool RevertToSelf();

....

Then, in your code you declare a variable:

IntPtr iToken;
if (LogonUser(user_name, remote_server_name (or ip), password, 9, 3, out
iToken) != 0)
{
ImpersonateLoggedOnUser(iToken);
//copy stuff
RevertToSelf();
}

Took me quite some time to work it down to all that.

I'd explain it a little more but I'm stuck for time right now.
HTH.

[quoted text, click to view]
Tarh ik
6/27/2005 7:26:10 AM

WOW!!! Thanks!!!!!

Senthamarai
7/8/2005 11:24:03 AM
That works great!

But I have a different requirement: I will have videos (WMV Files) stored
in a network share, I want my program (WM Player) to impersonate a particular
user and access the video. I guest WM Player uses its own impersonation
(Logged in user).

I use the following code:

IntPtr iToken;
if (LogonUser(textBox2.Text, textBox1.Text, textBox3.Text, 9, 3, out iToken)
!= 0)
{
ImpersonateLoggedOnUser(iToken);
}

axWindowsMediaPlayer1.URL = "\\\\Server\\public\\Video.wmv";


[quoted text, click to view]
AddThis Social Bookmark Button