This article is an example of the ASP.net implementation access to the local area network shared directory of files under the solution, the complete code is as follows:
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, Strin
G 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 = @ "//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 tem
pwindowsidentity;
IntPtr token = IntPtr.Zero;
IntPtr tokenduplicate = IntPtr.Zero; if (RevertToSelf ()) {if Logonusera (userName, domain, password, logon32_logon_interactive, logon32_p RoVider_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 (); }
}