资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

asp.net项目中午法访问局域网共享目录中的文件如何解决-创新互联

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 = @"//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(); 
  } 
}

新闻名称:asp.net项目中午法访问局域网共享目录中的文件如何解决-创新互联
转载注明:http://www.cdkjz.cn/article/jipii.html
十二年 建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220