asp.net實(shí)現(xiàn)訪問(wèn)局域網(wǎng)共享目錄下文件的解決方法
更新時(shí)間:2014年07月18日 14:47:37 投稿:shichen2014
這篇文章主要介紹了asp.net實(shí)現(xiàn)訪問(wèn)局域網(wǎng)共享目錄下文件的解決方法,需要的朋友可以參考下
本文以實(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();
}
}
相關(guān)文章
Asp.net mvc 權(quán)限過(guò)濾和單點(diǎn)登錄(禁止重復(fù)登錄)
這篇文章主要介紹了Asp.net mvc 權(quán)限過(guò)濾和單點(diǎn)登錄(禁止重復(fù)登錄)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-12-12
asp.net LC.exe已退出代碼為 -1的原因分析及解決方法
錯(cuò)誤“LC.exe”已退出,代碼為 -1。是VS2005,并且在項(xiàng)目中引用了第三方組件。2013-06-06
Entity?Framework實(shí)體拆分多個(gè)表
這篇文章介紹了Entity?Framework實(shí)體拆分多個(gè)表的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03
介紹幾個(gè)ASP.NET中容易忽略但卻很重要的方法函數(shù)
介紹幾個(gè)ASP.NET中容易忽略但卻很重要的方法函數(shù)...2006-09-09

