ASP.NET·ÃÎÊÍøÂçÇý¶¯Æ÷£¨Ó³Éä´ÅÅÌ£©
Ò²ÐíºÜ¶àÅóÓÑÔÚ×öWEBÏîÄ¿µÄʱºò¶¼»áÅöµ½ÕâÑùÒ»¸öÐèÇó£º
µ±Óû§ÉÏ´«Îļþʱ£¬ÐèÒª½«ÉÏ´«µÄÎļþ±£´æµ½ÁíÍâһ̨רÃŵÄÎļþ·þÎñÆ÷¡£
ҪʵÏÖÕâÑùÒ»¸ö¹¦ÄÜ£¬ÓÐÁ½ÖÖ½â¾ö·½°¸£º
·½°¸Ò»¡¢ÔÚÎļþ·þÎñÆ÷ÉÏн¨Ò»Õ¾µã£¬ÓÃÀ´½ÓÊÕÉÏ´«µÄÎļþ£¬È»ºó±£´æ¡£
·½°¸¶þ¡¢½«Îļþ·þÎñÆ÷µÄÖ¸¶¨Ä¿Â¼¹²Ïí¸øWEB·þÎñÆ÷£¬ÓÃÀ´±£´æÎļþ¡£
·½°¸Ò»²»Óöà˵£¬Ó¦¸ÃÊǺܼòµ¥µÄÁË£¬½«ÉÏ´«ÎļþµÄFORM±íµ¥µÄACTIONÊôÐÔÖ¸ÏòÎļþ·þÎñÆ÷ÉϵÄÕ¾µã¼´¿É£¬ÎÒÃÇÀ´Öصã˵Ï·½°¸¶þ¡£
Ò²ÐíÄã»á˵£¬Æäʵ·½°¸¶þÒ²ºÜ¼òµ¥£¬ÔÚWEB·þÎñÆ÷ÉÏ×öÏ´ÅÅÌÓ³É䣬ȻºóÖ±½Ó·ÃÎʲ»¾ÍÐÐÁË¡£Æäʵ²»ÊÇÕâÑùµÄ£¬IISĬÈÏÕË»§Îª NETWORK_SERVICE£¬¸ÃÕË»§ÊÇûȨÏÞ·ÃÎʹ²ÏíĿ¼µÄ£¬ËùÒÔµ±Äã°ÑÕ¾µã²¿Êðµ½IISÉϵÄʱºò£¬ÔÙ·ÃÎÊÓ³Éä´ÅÅ̾ͻᱨ¡°ÕÒ²»µ½Â·¾¶¡±µÄ´íÎó¡£Ëù ÒÔ£¬Ö±½Ó´´½¨´ÅÅÌÓ³ÉäÊÇÐв»Í¨µÄ£¬ÎÒÃÇÐèÒªÔÚ³ÌÐòÖÐÓÃÖ¸¶¨ÕË»§´´½¨Ó³É䣬²¢ÓøÃÕË»§ÔËÐÐIIS½ø³Ì£¬ÏÂÃæÀ´ËµÏÂÏêϸ²½Öè¼°Ïà¹Ø´úÂë¡£
1¡¢ÔÚÎļþ·þÎñÆ÷ÉÏ´´½¨¹²ÏíĿ¼£¬²¢Ð½¨·ÃÎÊÕË»§¡£
±ÈÈç¹²ÏíĿ¼Ϊ£º\\192.168.0.9\share
·ÃÎÊÕË»§Îª£ºuser-1 ÃÜÂëΪ£º123456
2¡¢ÔÚWEB·þÎñÆ÷ÉÏн¨Óû§£ºuser-1 ÃÜÂëΪ£º123456£¬Óû§×éÑ¡ÔñĬÈϵÄuser×é¼´¿É¡£
3¡¢ÔÚWEBÏîÄ¿ÖÐн¨¹«¹²ÀàWNetHelper
using System.Runtime.InteropServices; 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; } }
4¡¢ÎªIISÖ¸¶¨ÔËÐÐÕË»§user-1
ҪʵÏִ˹¦ÄÜ£¬ÓÐÁ½ÖÖ°ì·¨£º
a) ÔÚweb.configÎļþÖеÄ<system.web>½ÚµãÏ£¬Ìí¼ÓÈçÏÂÅäÖãº<identity impersonate="true" userName="user-1" password="123456" />
b) ÔÚWEBÏîÄ¿ÖÐÌí¼Ó¹«ÓÃÀàLogonImpersonate
public class LogonImpersonate : 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 LogonImpersonate(string username, string password) { if (username.IndexOf("\\") == -1) { Init(username, password, DefaultDomain); } else { string[] pair = username.Split(new char[] { '\\' }, 2); Init(pair[1], password, pair[0]); } } public LogonImpersonate(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)); } } ~LogonImpersonate() { Dispose(); } public void Dispose() { if (context != null) { try { context.Undo(); } finally { CloseHandle(token); context = null; } } } }
ÔÚ·ÃÎÊÓ³Éä´ÅÅÌ֮ǰÊ×Ïȵ÷ÓôËÀàΪIIS¸ü»»ÔËÐÐÓû§£ºLogonImpersonate imper = new LogonImpersonate("user-1", "123456");
5¡¢ÔÚ·ÃÎʹ²ÏíĿ¼ǰ£¬µ÷ÓÃWNetHelper.WNetAddConnection£¬Ìí¼Ó´ÅÅÌÓ³Éä
public static bool CreateDirectory(string path) { uint state = 0; if (!Directory.Exists("Z:")) { state = WNetHelper.WNetAddConnection(@"comp-1\user-1", "123456", @"\\192.168.0.9\share", "Z:"); } if (state.Equals(0)) { Directory.CreateDirectory(path); return true; } else { throw new Exception("Ìí¼ÓÍøÂçÇý¶¯Æ÷´íÎ󣬴íÎóºÅ£º" + state.ToString()); } }
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");
Óв»Ã÷°×µÄ¿ÉÒÔ·¢EMAIL¸øÎÒ£ºsqzhuyi@gmail.com
ÍƼöÐÅÏ¢
ÈÈÃÅÐÅÏ¢
- nohup: redirecting stderr to stdou....
- ʹÓÃlog_formatΪNginx·þÎñÆ÷ÉèÖøüÏêϸµÄÈÕÖ¾¸ñʽ
- jquery easyUI--dataGrid-Json
- [Ô´´]·ÂGoogle Reader¡¢ÐÂÀË΢²©¡¢ÌÚѶ΢²©µ....
- ÀûÓÃKeepalived+mysql¹¹½¨¸ß¿ÉÓÃMySQLË«Ö÷×Ô¶....
- Nginx+keepalivedʵÏÖ¸ºÔؾùºâºÍË«»úÈȱ¸¸ß¿ÉÓÃ
- jqueryʵÏÖÒ³Ãæ¼ÓÔؽø¶ÈÌõ
- Rolling cURL: PHP²¢·¢×î¼Ñʵ¼ù
- codeigniter ·ÓÉÖÕ¼«ÓÅ»¯(url rewrite)
- linuxÏÂÉèÖÃsshÎÞÃÜÂëµÇ¼
×î½ü¸üÐÂ
- Asp.net MVCÔ´Âë·ÖÎö--Model Validation(Se....
- .Net À¬»ø»ØÊÕ»úÖÆÔÀíºÍËã·¨£¨Ò»£©
- .netʵÌåнâ
- ·ÖÎöASP.NET¶ÁÈ¡XMLÎļþ4ÖÖ·½·¨
- »ñÈ¡ÍøÕ¾ËõÂÔͼ´úÂë
- Asp.net ¸ù¾ÝIPµØÖ·»ñÈ¡¿çÍø¶ÎmacµØÖ·º¯Êý¡¾ËѲء¿
- ¹ýÂËÃô¸Ð¹Ø¼ü×Ö¡£
- Éú³ÉÖ¸¶¨Î»ÊýµÄËæ»ú×Ö·û´®
- ¿Í»§¶Ë»ñÈ¡MACµØÖ·£¬µ½·þÎñÆ÷ÑéÖ¤
- ²½²½ÎªÓª .NETÈý²ã¼Ü¹¹½âÎö ¶þ¡¢Êý¾Ý¿âÉè¼Æ
ÆÀÂÛ