首页 > 代码库 > C#用域账号登陆,访问网络路径

C#用域账号登陆,访问网络路径

使用域账号登陆,访问网络路径

 1 public class FileTool : IDisposable
 2 {
 3 [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
 4 private static extern bool CloseHandle(IntPtr handle);
 5 [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
 6 private static extern int DuplicateToken(IntPtr hToken, int impersonationLevel, ref IntPtr hNewToken);
 7 [DllImport("advapi32.dll", SetLastError = true)]
 8 private static extern int LogonUser(string lpszUserName, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
 9 [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
10 private static extern bool RevertToSelf();
11 
12 private const int LOGON32_LOGON_INTERACTIVE = 2;
13 private const int LOGON32_PROVIDER_DEFAULT = 0;
14 private WindowsImpersonationContext impersonationContext;
15 private IntPtr _oldTokenHandle = IntPtr.Zero;
16 
17 /// <summary>
18 /// 通过Win API切换用户
19 /// </summary>
20 /// <param name="oldTokenHandle">oldTokenHandle</param>
21 /// <param name="userName">用户名</param>
22 /// <param name="domain">域名</param>
23 /// <param name="password">密码</param>
24 public FileTool(IntPtr oldTokenHandle, string userName, string domain, string password)
25 {
26 WindowsIdentity tempWindowsIdentity = null;
27 IntPtr token = IntPtr.Zero;
28 IntPtr tokenDuplicate = IntPtr.Zero;
29 _oldTokenHandle = oldTokenHandle;
30 
31 try
32 {
33 if (RevertToSelf())
34 {
35 if (LogonUser(userName, domain, password,
36 LOGON32_LOGON_INTERACTIVE,
37 LOGON32_PROVIDER_DEFAULT,
38 ref token) != 0)
39 {
40 if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
41 {
42 tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
43 impersonationContext = tempWindowsIdentity.Impersonate();
44 }
45 else
46 {
47 }
48 }
49 else
50 {
51 }
52 }
53 else
54 {
55 }
56 }
57 finally
58 {
59 if (token != IntPtr.Zero)
60 {
61 CloseHandle(token);
62 }
63 if (tokenDuplicate != IntPtr.Zero)
64 {
65 CloseHandle(tokenDuplicate);
66 }
67 }
68 }
69 
70 /// <summary>
71 /// 切换回原来的用户
72 /// </summary>
73 public void UndoImpersonation()
74 {
75 if (impersonationContext != null)
76 {
77 impersonationContext.Undo();
78 WindowsIdentity tempWindowsIdentity = new WindowsIdentity(_oldTokenHandle);
79 tempWindowsIdentity.Impersonate();
80 }
81 }
82 
83 /// <summary>
84 /// 释放
85 /// </summary>
86 public void Dispose()
87 {
88   UndoImpersonation();
89 }
90 }

 

调用

 1     public static FileTool DoFileTool()
 2         {
 3             return new FileTool(WindowsIdentity.GetCurrent().Token, "username", "domain", "password");
 4         }
 5 
 6         private void Form1_Load(object sender, EventArgs e)
 7         {
 8             using (FileTool pp = DoFileTool())
 9             {
10                 try
11                 {
12                     File.Copy(@"C:\Users\AAA\Desktop\Alice\DLEQ_Import.7z", @"\\SZXXX\TEST20170630\DLEQ_Import.7z", true);
13                 }
14                 catch (Exception ex)
15                 {
16                     MessageBox.Show(ex.Message);
17                 }
18             }
19         }

 

C#用域账号登陆,访问网络路径