也許很多朋友在做WEB項(xiàng)目的時候都會碰到這樣一個需求:當(dāng)用戶上傳文件時,需要將上傳的文件保存到另外一臺專門的文件服務(wù)器。
要實(shí)現(xiàn)這樣一個功能,有兩種解決方案:
方案一、在文件服務(wù)器上新建一站點(diǎn),用來接收上傳的文件,然后保存。
方案二、將文件服務(wù)器的指定目錄共享給WEB服務(wù)器,用來保存文件。
方案一不用多說,應(yīng)該是很簡單的了,將上傳文件的FORM表單的ACTION屬性指向文件服務(wù)器上的站點(diǎn)即可,我們來重點(diǎn)說下方案二。
也許你會說,其實(shí)方案二也很簡單,在WEB服務(wù)器上做下磁盤映射,然后直接訪問不就行了。其實(shí)不是這樣的,IIS默認(rèn)賬戶為NETWORK_SERVICE,該賬戶是沒權(quán)限訪問共享目錄的,所以當(dāng)你把站點(diǎn)部署到IIS上的時候,再訪問映射磁盤就會報“找不到路徑”的錯誤。所以,直接創(chuàng)建磁盤映射是行不通的,我們需要在程序中用指定賬戶創(chuàng)建映射,并用該賬戶運(yùn)行IIS進(jìn)程,下面來說下詳細(xì)步驟及相關(guān)代碼。
1、在文件服務(wù)器上創(chuàng)建共享目錄,并新建訪問賬戶。
比如共享目錄為:\\192.168.0.9\share
訪問賬戶為:user-1 密碼為:123456
2、在WEB服務(wù)器上新建用戶:user-1 密碼為:123456,用戶組選擇默認(rèn)的user組即可。
3、在WEB項(xiàng)目中新建公共類WNetHelper
01 using System.Runtime.InteropServices;
02
03 public class WNetHelper
04 {
05 [DllImport("mpr.dll", EntryPoint = "WNetAddConnection2")]
06 private static extern uint WNetAddConnection2(NetResource lpNetResource, string lpPassword, string lpUsername, uint dwFlags);
07
08 [DllImport("Mpr.dll", EntryPoint = "WNetCancelConnection2")]
09 private static extern uint WNetCancelConnection2(string lpName, uint dwFlags, bool fForce);
10
11 [StructLayout(LayoutKind.Sequential)]
12 public class NetResource
13 {
14 public int dwScope;
15
16 public int dwType;
17
18 public int dwDisplayType;
19
20 public int dwUsage;
21
22 public string lpLocalName;
23
24 public string lpRemoteName;
25
26 public string lpComment;
27
28 public string lpProvider;
29 }
30
31 /// <summary>
32 /// 為網(wǎng)絡(luò)共享做本地映射
33 /// </summary>
34 /// <param name="username">訪問用戶名(windows系統(tǒng)需要加計算機(jī)名,如:comp-1\user-1)</param>
35 /// <param name="password">訪問用戶密碼</param>
36 /// <param name="remoteName">網(wǎng)絡(luò)共享路徑(如:\\192.168.0.9\share)</param>
37 /// <param name="localName">本地映射盤符</param>
38 /// <returns></returns>
39 public static uint WNetAddConnection(string username, string password, string remoteName, string localName)
40 {
41 NetResource netResource = new NetResource();
42
43 netResource.dwScope = 2;
44 netResource.dwType = 1;
45 netResource.dwDisplayType = 3;
46 netResource.dwUsage = 1;
47 netResource.lpLocalName = localName;
48 netResource.lpRemoteName = remoteName.TrimEnd('\\');
49 uint result = WNetAddConnection2(netResource, password, username, 0);
50
51 return result;
52 }
53
54 public static uint WNetCancelConnection(string name, uint flags, bool force)
55 {
56 uint nret = WNetCancelConnection2(name, flags, force);
57
58 return nret;
59 }
60 }
4、為IIS指定運(yùn)行賬戶user-1
要實(shí)現(xiàn)此功能,有兩種辦法:
a) 在web.config文件中的<system.web>節(jié)點(diǎn)下,添加如下配置:<identity impersonate="true" userName="user-1" password="123456" />
b) 在WEB項(xiàng)目中添加公用類LogonImpersonate
01 public class LogonImpersonate : IDisposable
02 {
03 static public string DefaultDomain
04 {
05 get
06 {
07 return ".";
08 }
09 }
10
11 const int LOGON32_LOGON_INTERACTIVE = 2;
12 const int LOGON32_PROVIDER_DEFAULT = 0;
13
14 [System.Runtime.InteropServices.DllImport("Kernel32.dll")]
15 extern static int FormatMessage(int flag, ref IntPtr source, int msgid, int langid, ref string buf, int size, ref IntPtr args);
16
17 [System.Runtime.InteropServices.DllImport("Kernel32.dll")]
18 extern static bool CloseHandle(IntPtr handle);
19
20 [System.Runtime.InteropServices.DllImport("Advapi32.dll", SetLastError = true)]
21 extern static bool LogonUser(
22 string lpszUsername,
23 string lpszDomain,
24 string lpszPassword,
25 int dwLogonType,
26 int dwLogonProvider,
27 ref IntPtr phToken
28 );
29
30 IntPtr token;
31 System.Security.Principal.WindowsImpersonationContext context;
32
33 public LogonImpersonate(string username, string password)
34 {
35 if (username.IndexOf("\\") == -1)
36 {
37 Init(username, password, DefaultDomain);
38 }
39 else
40 {
41 string[] pair = username.Split(new char[] { '\\' }, 2);
42 Init(pair[1], password, pair[0]);
43 }
44 }
45 public LogonImpersonate(string username, string password, string domain)
46 {
47 Init(username, password, domain);
48 }
49 void Init(string username, string password, string domain)
50 {
51 if (LogonUser(username, domain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref token))
52 {
53 bool error = true;
54 try
55 {
56 context = System.Security.Principal.WindowsIdentity.Impersonate(token);
57 error = false;
58 }
59 finally
60 {
61 if (error)
62 CloseHandle(token);
63 }
64 }
65 else
66 {
67 int err = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
68
69 IntPtr tempptr = IntPtr.Zero;
70 string msg = null;
71
72 FormatMessage(0x1300, ref tempptr, err, 0, ref msg, 255, ref tempptr);
73
74 throw (new Exception(msg));
75 }
76 }
77 ~LogonImpersonate()
78 {
79 Dispose();
80 }
81 public void Dispose()
82 {
83 if (context != null)
84 {
85 try
86 {
87 context.Undo();
88 }
89 finally
90 {
91 CloseHandle(token);
92 context = null;
93 }
94 }
95 }
96 }
在訪問映射磁盤之前首先調(diào)用此類為IIS更換運(yùn)行用戶:LogonImpersonate imper = new LogonImpersonate("user-1", "123456");
5、在訪問共享目錄前,調(diào)用WNetHelper.WNetAddConnection,添加磁盤映射
01 public static bool CreateDirectory(string path)
02 {
03 uint state = 0;
04 if (!Directory.Exists("Z:"))
05 {
06 state = WNetHelper.WNetAddConnection(@"comp-1\user-1", "123456", @"\\192.168.0.9\share", "Z:");
07 }
08 if (state.Equals(0))
09 {
10 Directory.CreateDirectory(path);
11 return true;
12 }
13 else
14 {
15 throw new Exception("添加網(wǎng)絡(luò)驅(qū)動器錯誤,錯誤號:" + state.ToString());
16 }
17 }
6、完成。
簡潔代碼就是:
LogonImpersonate imper = new LogonImpersonate("user-1", "123456");
WNetHelper.WNetAddConnection(@"comp-1\user-1", "123456", @"\\192.168.0.9\share", "Z:");
Directory.CreateDirectory(@"Z:\newfolder");
file1.SaveAs(@"Z:\newfolder\test.jpg