博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
通过身份模拟实现远程资源访问
阅读量:5167 次
发布时间:2019-06-13

本文共 4752 字,大约阅读时间需要 15 分钟。

以下内容来源:

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Runtime.InteropServices; 6  7 namespace WebApplication1 8 { 9     public class WNetHelper10     {11 12         #region 通过身份模拟实现远程资源访问13 14         // logon types15         const int LOGON32_LOGON_INTERACTIVE = 2;16         const int LOGON32_LOGON_NETWORK = 3;17         const int LOGON32_LOGON_NEW_CREDENTIALS = 9;18         // logon providers19         const int LOGON32_PROVIDER_DEFAULT = 0;20         const int LOGON32_PROVIDER_WINNT50 = 3;21         const int LOGON32_PROVIDER_WINNT40 = 2;22         const int LOGON32_PROVIDER_WINNT35 = 1;23 24         [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]25         public static extern int LogonUser(String lpszUserName,26             String lpszDomain,27             String lpszPassword,28             int dwLogonType,29             int dwLogonProvider,30             ref IntPtr phToken);31 32         [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]33         public static extern int DuplicateToken(IntPtr hToken,34             int impersonationLevel,35             ref IntPtr hNewToken);36 37         [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]38         public static extern bool RevertToSelf();39 40         [DllImport("kernel32.dll", CharSet = CharSet.Auto)]41         public static extern bool CloseHandle(IntPtr handle);42 43         private static System.Security.Principal.WindowsImpersonationContext impersonationContext;44         /// 45         /// 连接网络资源46         /// 47         /// IP/计算机名48         /// 用户名49         /// 密码50         /// 
51 public static bool impersonateValidUser(String domain, String userName, String password)52 {53 System.Security.Principal.WindowsIdentity tempWindowsIdentity;54 IntPtr token = IntPtr.Zero;55 IntPtr tokenDuplicate = IntPtr.Zero;56 57 if (RevertToSelf())58 {59 // 这里使用LOGON32_LOGON_NEW_CREDENTIALS来访问远程资源。60 // 如果要(通过模拟用户获得权限)实现服务器程序,访问本地授权数据库可61 // 以用LOGON32_LOGON_INTERACTIVE62 if (LogonUser(userName, domain, password, LOGON32_LOGON_NEW_CREDENTIALS,63 LOGON32_PROVIDER_DEFAULT, ref token) != 0)64 {65 if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)66 {67 tempWindowsIdentity = new System.Security.Principal.WindowsIdentity(tokenDuplicate);68 impersonationContext = tempWindowsIdentity.Impersonate();69 if (impersonationContext != null)70 {71 System.AppDomain.CurrentDomain.SetPrincipalPolicy(System.Security.Principal.PrincipalPolicy.WindowsPrincipal);72 System.Security.Principal.IPrincipal pr = System.Threading.Thread.CurrentPrincipal;73 System.Security.Principal.IIdentity id = pr.Identity;74 CloseHandle(token);75 CloseHandle(tokenDuplicate);76 return true;77 }78 }79 }80 }81 if (token != IntPtr.Zero)82 CloseHandle(token);83 if (tokenDuplicate != IntPtr.Zero)84 CloseHandle(tokenDuplicate);85 return false;86 }87 88 public static void undoImpersonation()89 {90 impersonationContext.Undo();91 }92 93 #endregion94 }95 }
View Code
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7 using System.IO; 8  9 namespace WebApplication110 {11     public partial class WebForm1 : System.Web.UI.Page12     {13         protected void Page_Load(object sender, EventArgs e)14         {15             TestFunc();16           17         }18 19         public void TestFunc()20         {21             bool isImpersonated = false;22             try23             {24                 if (WNetHelper.impersonateValidUser("Netnetnet-pc", "admin2", "123"))25                 {26                     isImpersonated = true;27                     System.IO.File.Copy(@"\\Netnetnet-pc\站点发布\a.txt", "d:\\a1.txt", true);28                 }29             }30             finally31             {32                 if (isImpersonated)33                     WNetHelper.undoImpersonation();34             }35         }36     }37 }
调用

 

转载于:https://www.cnblogs.com/yf2011/p/4126387.html

你可能感兴趣的文章
跨浏览器的事件处理程序
查看>>
document.write
查看>>
ASP.Net
查看>>
[Java]读取文件方法(转)
查看>>
React 从入门到进阶之路(六)
查看>>
ACM牛人博客
查看>>
二分搜索 Codeforces Round #218 (Div. 2) C. Hamburgers
查看>>
Codeforces Round #336 (Div. 2)
查看>>
gunicorn运行显示connection in use解决办法
查看>>
CSUST 1506 ZZ的计算器 模拟题
查看>>
2013“嘉杰信息”杯ACM/ICPC湘潭多省程序设计竞赛暨湘潭市第五届大学生程序设计竞赛 F题 five tiger 湘潭大学1173题...
查看>>
20个国外创意404错误页面设计
查看>>
【原创】11. MYSQL++ 之 Quoting 与 Escaping
查看>>
生成验证码图片与验证码刷新
查看>>
MySQL开发设计规范
查看>>
数据结构之图(图的基本操作)
查看>>
HDU 1005 Number Sequence
查看>>
666_2010
查看>>
vim E212
查看>>
.net core 在 Docker 上的部署
查看>>