asp.net实现访问局域网共享目录下文件的解决方法
2023-06-13 09:15:38 时间
本文以实例讲述了asp.net实现访问局域网共享目录下文件的解决方法,完整代码如下所示:
usingSystem; usingSystem.Collections; usingSystem.Configuration; usingSystem.Data; usingSystem.Linq; usingSystem.Web; usingSystem.Web.Security; usingSystem.Web.UI; usingSystem.Web.UI.HtmlControls; usingSystem.Web.UI.WebControls; usingSystem.Web.UI.WebControls.WebParts; usingSystem.Xml.Linq; usingSystem.IO; usingSystem.Security.Principal; usingSystem.Runtime.InteropServices; publicpartialclass_Default:System.Web.UI.Page { publicconstintLOGON32_LOGON_INTERACTIVE=2; publicconstintLOGON32_PROVIDER_DEFAULT=0; WindowsImpersonationContextimpersonationContext; [DllImport("advapi32.dll")] publicstaticexternintLogonUserA(StringlpszUserName, StringlpszDomain, StringlpszPassword, intdwLogonType, intdwLogonProvider, refIntPtrphToken); [DllImport("advapi32.dll",CharSet=CharSet.Auto,SetLastError=true)] publicstaticexternintDuplicateToken(IntPtrhToken, intimpersonationLevel, refIntPtrhNewToken); [DllImport("advapi32.dll",CharSet=CharSet.Auto,SetLastError=true)] publicstaticexternboolRevertToSelf(); [DllImport("kernel32.dll",CharSet=CharSet.Auto)] publicstaticexternboolCloseHandle(IntPtrhandle); publicvoidPage_Load(Objects,EventArgse) { if(impersonateValidUser("lucas","Workgroup","lcas")) { stringpath=@"//zhehui001/lu"; foreach(stringfinDirectory.GetFiles(path)) { Response.Write(f); } undoImpersonation(); } else { //Yourimpersonationfailed.Therefore,includeafail-safemechanismhere. } } privateboolimpersonateValidUser(StringuserName,Stringdomain,Stringpassword) { WindowsIdentitytempWindowsIdentity; IntPtrtoken=IntPtr.Zero; IntPtrtokenDuplicate=IntPtr.Zero; if(RevertToSelf()) { if(LogonUserA(userName,domain,password,LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT,reftoken)!=0) { if(DuplicateToken(token,2,reftokenDuplicate)!=0) { tempWindowsIdentity=newWindowsIdentity(tokenDuplicate); impersonationContext=tempWindowsIdentity.Impersonate(); if(impersonationContext!=null) { CloseHandle(token); CloseHandle(tokenDuplicate); returntrue; } } } } if(token!=IntPtr.Zero) CloseHandle(token); if(tokenDuplicate!=IntPtr.Zero) CloseHandle(tokenDuplicate); returnfalse; } privatevoidundoImpersonation() { impersonationContext.Undo(); } }
相关文章
- asp.net mvc实现文件下载「建议收藏」
- ASP.NET Core 6框架揭秘实例演示[29]:搭建文件服务器
- ASP.NET WEB——项目创建与文件上传操作
- 应用使用Linux环境部署.Net应用程序(linux部署.net)
- 快速搭建 NET 项目,用 MySQL 配置环境(.net 配置mysql)
- NET 对MySQL 的支持有限(.net不支持mysql)
- ASP.NET中文件上传下载方法集合
- ASP.NET通过Remotingservice上传文件
- 在ASP.NET中实现多文件上传的方法
- asp.net下大文件上传知识整理
- asp.net下文件上传和文件删除的代码
- asp.net(C#)中上传大文件的几中常见应用方法
- asp.net读取xml文件里面的内容,绑定到dropdownlist中
- 手动把asp.net的类生成dll文件的方法
- ASP.NET、SharePoint中另存文件的长文件名被截断的原因及解决办法
- 点击图片,AJAX删除后台图片文件的实现代码(asp.net)
- asp.net实现文件下载的代码
- asp.net未能加载文件或程序集“XXX”或它的某一个依赖项。试图加载格式不正确的程序。
- .Net中导出数据到Excel(asp.net和winform程序中)
- Asp.net在ashx文件中处理Session问题解决方法
- .Net实现合并文件的具体方法
- asp.net文件上传示例分享
- ASP.NET批量下载文件的方法
- .net实现文件读写的几种常用方法
- asp.net(C#)禁止缓存文件不让文件缓存到客户端