programatically use specific criteria to access network folder

hi

I am about to create a program which is going to work on some folder on computers in the network. If I log in as admin then I can access them. if not I dont have permission. now I want my program independet of the user who loged in. I want to use a specific user criteria even if the logged in user is defferent. how can I do it?

thank you in advance

[375 byte] By [ashk1860] at [2008-1-7]
# 1
in my recent search I think my solution is impersonation. but I can't find a clear and good example in the samples. they mostly more complicated than I can follow easily. just suppose that I have a user "admin" with pass "123" how can I impersonate and use its criteria to access the folder that is granted for this user?

any help will be appreciated

ashk1860 at 2007-10-2 > top of Msdn Tech,Visual C#,Visual C# General...
# 2

here is the answer (easiest code that I find and make some changes to get it handy):

public class ImpersonateUser

{

const int LOGON32_LOGON_INTERACTIVE = 2;

const int LOGON32_LOGON_NETWORK = 3;

const int LOGON32_LOGON_BATCH = 4;

const int LOGON32_LOGON_SERVICE = 5;

const int LOGON32_LOGON_UNLOCK = 7;

const int LOGON32_LOGON_NETWORK_CLEARTEXT = 8;

const int LOGON32_LOGON_NEW_CREDENTIALS = 9;

const int LOGON32_PROVIDER_DEFAULT = 0;

[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", SetLastError = true)]

public static extern int ImpersonateLoggedOnUser(

IntPtr hToken

);

[DllImport("advapi32.dll", SetLastError = true)]

static extern int RevertToSelf();

[DllImport("kernel32.dll", SetLastError = true)]

static extern int CloseHandle(IntPtr hObject);

public static bool ImpersonateTo(string username,string domain,string pass)

{

IntPtr lnToken;

int TResult = LogonUser(username, domain, pass, LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, out lnToken);

if (TResult > 0)

{

ImpersonateLoggedOnUser(lnToken);

StringBuilder sb = new StringBuilder(80, 80);

CloseHandle(lnToken);

return true;

}

else

{

return false;

}

return false;

}

}

ashk1860 at 2007-10-2 > top of Msdn Tech,Visual C#,Visual C# General...