濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > asp.net實(shí)現(xiàn)訪問(wèn)局域網(wǎng)共享目錄下文件的解決方法

asp.net實(shí)現(xiàn)訪問(wèn)局域網(wǎng)共享目錄下文件的解決方法

熱門標(biāo)簽:400電話如何申請(qǐng)取消 柳州電銷機(jī)器人公司 昆明語(yǔ)音電銷機(jī)器人價(jià)格 電銷語(yǔ)音機(jī)器人型號(hào)參數(shù) 太原400電話上門辦理 征途美甲店地圖標(biāo)注 騰訊地圖標(biāo)注手機(jī) 百度地圖怎樣做地圖標(biāo)注 浦發(fā)電話機(jī)器人提醒還款

本文以實(shí)例講述了asp.net實(shí)現(xiàn)訪問(wèn)局域網(wǎng)共享目錄下文件的解決方法,完整代碼如下所示:

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服務(wù)實(shí)現(xiàn)軟件共享
  • ASP.NET訪問(wèn)共享文件夾的詳細(xì)步驟

標(biāo)簽:天門 白山 蘭州 陽(yáng)泉 德陽(yáng) 張家界 江蘇 新疆

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《asp.net實(shí)現(xiàn)訪問(wèn)局域網(wǎng)共享目錄下文件的解決方法》,本文關(guān)鍵詞  asp.net,實(shí)現(xiàn),訪問(wèn),局域網(wǎng),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《asp.net實(shí)現(xiàn)訪問(wèn)局域網(wǎng)共享目錄下文件的解決方法》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于asp.net實(shí)現(xiàn)訪問(wèn)局域網(wǎng)共享目錄下文件的解決方法的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    屏边| 裕民县| 阿坝| 开封县| 黎平县| 吴堡县| 宁强县| 神木县| 会同县| 泰安市| 清丰县| 广丰县| 达州市| 城口县| 辽阳市| 吉木萨尔县| 门源| 莒南县| 陆河县| 勐海县| 齐齐哈尔市| 巫山县| 潼关县| 中卫市| 青海省| 鹤岗市| 泽州县| 沂源县| 泉州市| 乐安县| 绍兴市| 格尔木市| 永善县| 资溪县| 赞皇县| 昭平县| 秦皇岛市| 石首市| 灵川县| 宽甸| 深泽县|