The following source: http://www.cnblogs.com/h2appy/articles/1204277.html
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Runtime.InteropServices;6 7 namespaceWebApplication18 {9 Public classWnethelperTen { One A #regionRemote resource access through identity impersonation - - //Logon Types the Const intLogon32_logon_interactive =2; - Const intLogon32_logon_network =3; - Const intLogon32_logon_new_credentials =9; - //Logon Providers + Const intLogon32_provider_default =0; - Const intLogon32_provider_winnt50 =3; + Const intLOGON32_PROVIDER_WINNT40 =2; A Const intLogon32_provider_winnt35 =1; at -[DllImport ("Advapi32.dll", CharSet = CharSet.Auto, SetLastError =true)] - Public Static extern intLogonUser (String lpszUserName, - String Lpszdomain, - String Lpszpassword, - intdwLogonType, in intdwLogonProvider, - refIntPtr phtoken); to +[DllImport ("Advapi32.dll", CharSet = CharSet.Auto, SetLastError =true)] - Public Static extern intDuplicateToken (IntPtr htoken, the intImpersonationLevel, * refIntPtr hNewToken); $ Panax Notoginseng[DllImport ("Advapi32.dll", CharSet = CharSet.Auto, SetLastError =true)] - Public Static extern BOOLRevertToSelf (); the +[DllImport ("Kernel32.dll", CharSet =CharSet.Auto)] A Public Static extern BOOLCloseHandle (IntPtr handle); the + Private StaticSystem.Security.Principal.WindowsImpersonationContext impersonationcontext; - /// <summary> $ ///Connecting network resources $ /// </summary> - /// <param name= "domain" >ip/Computer name</param> - /// <param name= "UserName" >User name</param> the /// <param name= "password" >Password</param> - /// <returns></returns>Wuyi Public Static BOOLimpersonateValidUser (string domain, string userName, string password) the { - System.Security.Principal.WindowsIdentity tempwindowsidentity; WuINTPTR token =IntPtr.Zero; -IntPtr tokenduplicate =IntPtr.Zero; About $ if(RevertToSelf ()) - { - //Here you use Logon32_logon_new_credentials to access remote resources. - //if you want to implement a server program (by impersonating a user), access the local authorization database to A //to use logon32_logon_interactive + if(LogonUser (userName, domain, password, logon32_logon_new_credentials, theLogon32_provider_default,reftoken)! =0) - { $ if(DuplicateToken (token,2,reftokenduplicate)! =0) the { thetempWindowsIdentity =NewSystem.Security.Principal.WindowsIdentity (tokenduplicate); theimpersonationcontext =tempwindowsidentity.impersonate (); the if(Impersonationcontext! =NULL) - { in System.AppDomain.CurrentDomain.SetPrincipalPolicy (System.Security.Principal.PrincipalPolic Y.windowsprincipal); theSystem.Security.Principal.IPrincipal PR =System.Threading.Thread.CurrentPrincipal; theSystem.Security.Principal.IIdentity ID =PR. Identity; About CloseHandle (token); the CloseHandle (tokenduplicate); the return true; the } + } - } the }Bayi if(Token! =IntPtr.Zero) the CloseHandle (token); the if(Tokenduplicate! =IntPtr.Zero) - CloseHandle (tokenduplicate); - return false; the } the the Public Static voidundoimpersonation () the { - Impersonationcontext.undo (); the } the the #endregion94 } the}View Code
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingsystem.web;5 usingSystem.Web.UI;6 usingSystem.Web.UI.WebControls;7 usingSystem.IO;8 9 namespaceWebApplication1Ten { One Public Partial classWebForm1:System.Web.UI.Page A { - protected voidPage_Load (Objectsender, EventArgs e) - { the TestFunc (); - - } - + Public voidTestFunc () - { + BOOLisimpersonated =false; A Try at { - if(Wnethelper.impersonatevaliduser ("netnetnet-pc","admin2","123")) - { -isimpersonated =true; -System.IO.File.Copy (@"\\Netnetnet-pc\ site Release \a.txt","D:\\a1.txt",true); - } in } - finally to { + if(isimpersonated) - wnethelper.undoimpersonation (); the } * } $ }Panax Notoginseng}called
Remote resource access through identity impersonation