婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > asp.net實現訪問局域網共享目錄下文件的解決方法

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

熱門標簽:400電話如何申請取消 柳州電銷機器人公司 昆明語音電銷機器人價格 電銷語音機器人型號參數 太原400電話上門辦理 征途美甲店地圖標注 騰訊地圖標注手機 百度地圖怎樣做地圖標注 浦發電話機器人提醒還款

本文以實例講述了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(); 
  } 
}
您可能感興趣的文章:
  • jsp和asp.net共享session值示例代碼
  • ASP.NET中在不同的子域中共享Session的具體方法
  • ASP.NET+Web服務實現軟件共享
  • ASP.NET訪問共享文件夾的詳細步驟

標簽:天門 白山 蘭州 陽泉 德陽 張家界 江蘇 新疆

巨人網絡通訊聲明:本文標題《asp.net實現訪問局域網共享目錄下文件的解決方法》,本文關鍵詞  asp.net,實現,訪問,局域網,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《asp.net實現訪問局域網共享目錄下文件的解決方法》相關的同類信息!
  • 本頁收集關于asp.net實現訪問局域網共享目錄下文件的解決方法的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 阿合奇县| 浏阳市| 错那县| 客服| 永济市| 平潭县| 凤翔县| 丰台区| 琼结县| 墨竹工卡县| 平定县| 鹰潭市| 巩留县| 泽普县| 望城县| 南岸区| 临夏县| 平果县| 上虞市| 南安市| 西盟| 奇台县| 信阳市| 乌拉特后旗| 广德县| 台南市| 齐齐哈尔市| 蓝山县| 汉寿县| 方城县| 元朗区| 双峰县| 五指山市| 江阴市| 榆中县| 梁山县| 启东市| 邵阳县| 福建省| 寻甸| 奇台县|