国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

asp.net實現訪問局域網共享目錄下文件的解決方法

2024-07-10 12:43:16
字體:
來源:轉載
供稿:網友

本文以實例講述了asp.net實現訪問局域網共享目錄下文件的解決方法,完整代碼如下所示:

using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.IO; using System.Security.Principal; using System.Runtime.InteropServices; public partial class _Default : System.Web.UI.Page {   public const int LOGON32_LOGON_INTERACTIVE = 2;   public const int LOGON32_PROVIDER_DEFAULT = 0;   WindowsImpersonationContext impersonationContext;   [DllImport("advapi32.dll")]   public static extern int LogonUserA(String lpszUserName,     String lpszDomain,     String lpszPassword,     int dwLogonType,     int dwLogonProvider,     ref IntPtr phToken);   [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]   public static extern int DuplicateToken(IntPtr hToken,     int impersonationLevel,     ref IntPtr hNewToken);   [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]   public static extern bool RevertToSelf();   [DllImport("kernel32.dll", CharSet = CharSet.Auto)]   public static extern bool CloseHandle(IntPtr handle);   public void Page_Load(Object s, EventArgs e)   {     if (impersonateValidUser("lucas", "Workgroup", "lcas"))     {       string path = @"http://zhehui001/lu";       foreach (string f in Directory.GetFiles(path))       {         Response.Write(f);       }       undoImpersonation();     }     else     {       //Your impersonation failed. Therefore, include a fail-safe mechanism here.     }   }   private bool impersonateValidUser(String userName, String domain, String password)   {     WindowsIdentity tempWindowsIdentity;     IntPtr token = IntPtr.Zero;     IntPtr tokenDuplicate = IntPtr.Zero;     if (RevertToSelf())     {       if (LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE,         LOGON32_PROVIDER_DEFAULT, ref token) != 0)       {         if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)         {           tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);           impersonationContext = tempWindowsIdentity.Impersonate();           if (impersonationContext != null)           {             CloseHandle(token);             CloseHandle(tokenDuplicate);             return true;           }         }       }     }     if (token != IntPtr.Zero)       CloseHandle(token);     if (tokenDuplicate != IntPtr.Zero)       CloseHandle(tokenDuplicate);     return false;   }   private void undoImpersonation()   {     impersonationContext.Undo();   } }
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 南江县| 巴青县| 宁河县| 永吉县| 澄江县| 翼城县| 石楼县| 竹山县| 敦煌市| 黄陵县| 石林| 霍林郭勒市| 龙川县| 皮山县| 大余县| 深泽县| 庆阳市| 五峰| 乡城县| 湖北省| 房产| 高安市| 阳江市| 邛崃市| 翁牛特旗| 耒阳市| 沾益县| 子洲县| 凤山县| 桂阳县| 尤溪县| 佳木斯市| 开江县| 乐平市| 岚皋县| 卢龙县| 新平| 岚皋县| 修文县| 威海市| 甘南县|