項目中的文件須要保存到網絡存儲設備中,以前用的是NAS。因沒來得及採購就先用Samba頂上。代碼發現通用……linux
1、定義:windows
Samba是在Linux和UNIX系統上實現SMB協議的一個免費軟件,由服務器及客戶端程序構成。SMB(Server Messages Block,信息服務塊)是一種在局域網上共享文件和打印機的一種通訊協議,它爲局域網內的不一樣計算機之間提供文件及打印機等資源的共享服務。SMB協議是客戶機/服務器型協議,客戶機經過該協議能夠訪問服務器上的共享文件系統、打印機及其餘資源。經過設置「NetBIOS over TCP/IP」使得Samba不但能與局域網絡主機分享資源,還能與全世界的電腦分享資源。(百科)api
NAS(Network Attached Storage)網絡存儲基於標準網絡協議實現數據傳輸,爲網絡中的Windows / Linux / Mac OS 等各類不一樣操做系統的計算機提供文件共享和數據備份。(百科)服務器
2、兩者有什麼區別網絡
一、Samba是一個軟件,NAS是一個存儲解決方案dom
二、FTP基於windows平臺的,Samba是基於linux平臺的,NAS是基於存儲平臺的(硬件)ide
3、主要代碼LogonImpersonateHelper類用於IIS更換運行用戶、WNetHelper類用於建立網絡映射 ui
public class LogonImpersonateHelper : IDisposable { static public string DefaultDomain { get { return "."; } } const int LOGON32_LOGON_INTERACTIVE = 2; const int LOGON32_PROVIDER_DEFAULT = 0; [System.Runtime.InteropServices.DllImport("Kernel32.dll")] extern static int FormatMessage(int flag, ref IntPtr source, int msgid, int langid, ref string buf, int size, ref IntPtr args); [System.Runtime.InteropServices.DllImport("Kernel32.dll")] extern static bool CloseHandle(IntPtr handle); [System.Runtime.InteropServices.DllImport("Advapi32.dll", SetLastError = true)] extern static bool LogonUser( string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken ); IntPtr token; System.Security.Principal.WindowsImpersonationContext context; public LogonImpersonateHelper(string username, string password) { if (username.IndexOf("//") == -1) { Init(username, password, DefaultDomain); } else { string[] pair = username.Split(new char[] { '/' }, 2);//username.Split(new char[] { '//' }, 2); Init(pair[1], password, pair[0]); } } public LogonImpersonateHelper(string username, string password, string domain) { Init(username, password, domain); } void Init(string username, string password, string domain) { if (LogonUser(username, domain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref token)) { bool error = true; try { context = System.Security.Principal.WindowsIdentity.Impersonate(token); error = false; } finally { if (error) CloseHandle(token); } } else { int err = System.Runtime.InteropServices.Marshal.GetLastWin32Error(); IntPtr tempptr = IntPtr.Zero; string msg = null; FormatMessage(0x1300, ref tempptr, err, 0, ref msg, 255, ref tempptr); throw (new Exception(msg)); } } ~LogonImpersonateHelper() { Dispose(); } public void Dispose() { if (context != null) { try { context.Undo(); } finally { CloseHandle(token); context = null; } } } }
/// <summary> /// 網絡驅動器 映射幫助類 /// </summary> public class WNetHelper { [DllImport("mpr.dll", EntryPoint = "WNetAddConnection2")] private static extern uint WNetAddConnection2(NetResource lpNetResource, string lpPassword, string lpUsername, uint dwFlags); [DllImport("Mpr.dll", EntryPoint = "WNetCancelConnection2")] private static extern uint WNetCancelConnection2(string lpName, uint dwFlags, bool fForce); [StructLayout(LayoutKind.Sequential)] public class NetResource { public int dwScope; public int dwType; public int dwDisplayType; public int dwUsage; public string lpLocalName; public string lpRemoteName; public string lpComment; public string lpProvider; } /// <summary> /// 爲網絡共享作本地映射 /// </summary> /// <param name="username">訪問用戶名(windows系統須要加計算機名,如:comp-1/user-1)</param> /// <param name="password">訪問用戶密碼</param> /// <param name="remoteName">網絡共享路徑(如://192.168.0.9/share)</param> /// <param name="localName">本地映射盤符</param> /// <returns></returns> public static uint WNetAddConnection(string username, string password, string remoteName, string localName) { NetResource netResource = new NetResource(); netResource.dwScope = 2; netResource.dwType = 1; netResource.dwDisplayType = 3; netResource.dwUsage = 1; netResource.lpLocalName = localName; netResource.lpRemoteName = remoteName.TrimEnd('\\'); uint result = WNetAddConnection2(netResource, password, username, 0); return result; } public static uint WNetCancelConnection(string name, uint flags, bool force) { uint nret = WNetCancelConnection2(name, flags, force); return nret; } }
建立文件夾spa
/// <summary> /// Samba建立文件夾 /// </summary> /// <param name="path"></param> /// <returns></returns> public static bool CreateDirectory(string path) { uint state = 0; if (!Directory.Exists("Z:")) { string nasPath = ConfigHelper.NasPath; string root = Directory.GetDirectoryRoot(nasPath); state = WNetHelper.WNetAddConnection(ConfigHelper.NasAccount, ConfigHelper.NasPassword, ConfigHelper.NasPath, root.Replace("/", "").Replace("\\", "")); } if (state.Equals(0)) { Directory.CreateDirectory(path); return true; } else { throw new Exception("添加網絡驅動器錯誤,錯誤號:" + state.ToString()); } }
4、如何使用操作系統
//在訪問映射磁盤以前首先調用此類爲IIS更換運行用戶 LogonImpersonateHelper imper = new LogonImpersonateHelper("qewd", "1,23#$&@qw"); //建立網絡映射 WNetHelper.WNetAddConnection(ConfigHelper.NasAccount, ConfigHelper.NasPassword, ConfigHelper.NasPath, root.Replace("/", "").Replace("\\", ""));
5、問題來了
在本地怎麼跑怎麼成功,放到服務器上就來問題了。環境:windows server200八、IIS7 報錯:「未知的用戶名或錯誤密碼。」
折騰了半天,折騰的事情就不說了,作個筆記
解決方案:
一、IIS 7 標示改了最高權限(暫無 辦法先這樣解決)
二、把Samba帳號、密碼添加到計算機本地用戶組中
6、結果-成功