Using system;
Using system. Collections. Generic;
Using system. text;
Using system. componentmodel;
Using system. Security;
Using system. Security. Principal;
Using system. runtime;
Using system. runtime. interopservices;
Using system. Web;
Using system. Web. Security;
namespace impersonate
{< br> [structlayout (layoutkind. sequential, charset = charset. auto)]
struct _ use_info_2
{< br> internal string ui2_local;
internal string ui2_remote;
internal intptr ui2_password; // don't pass a string or stringbuilder here !!
internal uint ui2_status;
internal uint ui2_asg_type;
internal uint ui2_refcount;
internal uint ui2_usecount;
internal string ui2_username;
internal string ui2_domainname;
}< br> class winnet
{< br> [dllimport ("netapi32", charset = charset. auto, setlasterror = true),
suppressunmanagedcodesecurityattribute]
static extern int netuseadd (
string uncservername, // not used
int level, // use info struct level 1 or 2
intptr Buf, // buffer
ref int parmerror
);
const uint use_wildcard = 0 xffffffff;
// Establish a use record
Public static void userecord (string resource, string user, string
Password, string domain)
{
Int ret = 0;
Int paramerror = 0;
_ Use_info_2 use2 = new _ use_info_2 ();
Intptr pbuf = intptr. zero;
Use2.ui2 _ password = intptr. zero;
Try
{
Pbuf = marshal. allochglobal (marshal. sizeof (use2 ));
Use2.ui2 _ local = NULL;
Use2.ui2 _ asg_type = use_wildcard;
Use2.ui2 _ remote = resource;
Use2.ui2 _ password = marshal. stringtohglobalauto (password );
Use2.ui2 _ username = user;
Use2.ui2 _ domainname = domain;
Marshal. structuretoptr (use2, pbuf, true );
Ret = netuseadd (null, 2, pbuf, ref paramerror );
If (Ret! = 0)
{
Throw new exception (New
Win32exception (marshal. getlastwin32error (). Message );
}
}
Finally
{
Marshal. freehglobal (use2.ui2 _ password );
Marshal. freehglobal (pbuf );
}
}
}
class Program
{< br> [system. runtime. interopservices. dllimport ("advapi32.dll")]
Public static extern int logonuser (string lpszusername,
string lpszdomain,
string lpszpassword,
int dwlogontype,
int dwlogonprovider,
ref intptr phtoken);
[Dllimport ("advapi32.dll", charset = charset. Auto, setlasterror = true)]
Public static extern int duplicatetoken (intptr htoken,
Int impersonationlevel,
Ref intptr hnewtoken );
[Dllimport ("advapi32.dll", charset = charset. Auto, setlasterror = true)]
Public static extern bool reverttoself ();
[Dllimport ("kernel32.dll", charset = charset. Auto)]
Public static extern bool closehandle (intptr handle );
Const int logon32_provider_default = 0;
Const int logon32_logon_interactive = 2;
Static public windowsimpersonationcontext WIC;
// Static void main (string [] ARGs)
//{
// Intptr lntoken;
// If (impersonatevaliduser ("michaell", "cmp-0641", "Wilma "))
//{
// Using (WIC)
//{
// String dir = @ "\ cmp-0641 \ C $ \" + "test ";
// System. Io. Directory. createdirectory (DIR );
//}
// Stringbuilder sb = new stringbuilder (80, 80 );
// Reverttoself ();
//// Closehandle (lntoken );
//}
// Else
//{
//}
// Return;
//}
Static public bool impersonatevaliduser (string username, string domain, string password)
{
Windowsidentity wi;
Intptr token = intptr. zero;
Intptr tokenduplicate = intptr. zero;
If (reverttoself ())
{
If (logonuser (username, domain, password, logon32_logon_interactive,
Logon32_provider_default, ref token )! = 0)
{
If (duplicatetoken (token, 2, ref tokenduplicate )! = 0)
{
Wi = new windowsidentity (tokenduplicate );
WIC = wi. Impersonate ();
If (WIC! = NULL)
{
Closehandle (token );
Closehandle (tokenduplicate );
Return true;
}
}
}
}
If (Token! = Intptr. Zero)
Closehandle (token );
If (tokenduplicate! = Intptr. Zero)
Closehandle (tokenduplicate );
Return false;
}
}
Public class logonuser
{
// Logonuser Parameters
[Dllimport ("advapi32.dll")]
Private Static extern bool logonuser (string lpszusername,
String lpszdomain,
String lpszpassword,
Int dwlogontype,
Int dwlogonprovider,
Ref intptr phtoken );
// Closehandle parameters. When you are finished,
// Free the memory allocated for the handle.
[Dllimport ("kernel32.dll", charset = system. runtime. interopservices. charset. Auto)]
Private Static extern bool closehandle (intptr handle );
Public static windowsidentity getwindowsidentity (string pusername, string pdomain)
{
Return NULL;
}
Public static windowsidentity getwindowsidentity (string pusername, string pdomain, string ppassword)
{
Intptr tokenhandle = intptr. zero;
Try
{
Const int logon32_provider_default = 0;
Const int logon32_logon_network = 5;
// Call logonuser to obtain
// Handle to an access token
Bool returnvalue = logonuser (pusername, pdomain,
Ppassword,
Logon32_logon_network,
Logon32_provider_default,
Ref tokenhandle );
If (false = returnvalue)
{
Return NULL;
}
// check the identity
// console. writeline ("before impersonation:" +
// windowsidentity. getcurrent (). name);
// create a windowsidentity from the impersonation
// token, then impersonate the user.
windowsidentity newid;
newid = new windowsidentity (tokenhandle);
return newid;
}
Catch (exception ex)
{
// Todo log the exception message.
Return NULL;
}
}
}
}