当前位置:首页 > asp.net 局域网存放文件

asp.net 局域网存放文件

点击次数:762  更新日期:2010-12-28
\n

1public const int LOGON32_LOGON_INTERACTIVE = 2;
2 public const int LOGON32_PROVIDER_DEFAULT = 0;
3
4 WindowsImpersonationContext impersonationContext;
5
6 [DllImport("advapi32.dll", CharSet = CharSet.Auto)]
7 public static extern int LogonUser(String lpszUserName,
8 String lpszDomain,
9 String lpszPassword,
10 int dwLogonType,
11 int dwLogonProvider,
12 ref IntPtr phToken);
13 [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
14 public extern static int DuplicateToken(IntPtr hToken,
15 int impersonationLevel,
16 ref IntPtr hNewToken);
17
18
19 private bool impersonateValidUser(String userName, String domain, String password)
20 {
21
22 IntPtr token = IntPtr.Zero;
23 IntPtr tokenDuplicate = IntPtr.Zero;
24
25 if (LogonUser(userName, domain, password, LOGON32_LOGON_INTERACTIVE,
26 LOGON32_PROVIDER_DEFAULT, ref token) != 0)
27 {
28 if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
29 {
30 WindowsIdentity tempWindowsIdentity;
31 tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
32 impersonationContext = tempWindowsIdentity.Impersonate();
33 if (impersonationContext != null)
34 return true;
35 else
36 return false;
37 }
38 else
39 return false;
40 }
41 else
42 return false;
43 }
44 private void undoImpersonation()
45 {
46 impersonationContext.Undo();//回退为未更改前账户
47 }
48
49 protected void Button1_Click(object sender, EventArgs e)
50 {
51 //临时更改为 跟 网络硬盘相同用户名密码的账户(此账户必须在网络盘有写入权限)本机也需要同样帐号密码的帐户
52
53 if (impersonateValidUser(“test”, “192.168.18.203″, “1111″))
54 {
55 //登陆后处理密码
56 if (!Directory.Exists(@”\\192.168.18.203sp est”))
57 {
58 try
59 {
60 Directory.CreateDirectory(@”\\192.168.18.203sp est”);
61 }
62 catch (Exception e1)
63 {
64 Response.Write(e1.Message);
65 }
66 }
67 FileUpload1.SaveAs(@”\\192.168.18.203sp estq.txt”);
68 undoImpersonation();
69 }
70 else
71 {
72 Response.Write(“登陆失败”);
73 //失败后处理密码
74 }
75 }

\n