asp.net访问网络路径方法(模拟用户登录)

核心代码

logonUser(string pszUsername,string pszDomain,string pszPassword,int dwlogonType,int dwlogonProvider,ref IntPtr phToken); // closes open handes returned by logonUser [DllImport("kernel32.dll",CharSet = CharSet.Auto)] extern static bool CloseHandle(IntPtr handle);
[DllImport("Advapi32.DLL")]
static extern bool ImpersonateLoggedOnUser(IntPtr hToken);
[DllImport("Advapi32.DLL")]
static extern bool RevertToSelf();
const int <a href="https://www.f2er.com/tag/logo/" target="_blank" class="keywords">logo</a>N32_PROVIDER_DEFAULT = 0;
const int <a href="https://www.f2er.com/tag/logo/" target="_blank" class="keywords">logo</a>N32_<a href="https://www.f2er.com/tag/logo/" target="_blank" class="keywords">logo</a>N_NEWCREDENTIALS = 9;//域ò控?中D的?需è要a用?:Interactive = 2
private bool disposed;
/// <summary>
/// 登?录?
/// </summary>
/// <param name="sUsername"&gt;用?户§名?</param>
/// <param name="sDomain"&gt;域ò名?,?如?果?不?在ú域ò中D就í使1用?机ú器÷IP地?址·</param>
/// <param name="sPassword"&gt;密ü码?</param>
public IdentityScope(string sUsername,string sDomain,string sPassword)
{
  // initialize tokens
  IntPtr pExistingTokenHandle = new IntPtr(0);
  IntPtr pDuplicateTokenHandle = new IntPtr(0);
  try
  {
    // get handle to token
    bool bImpersonated = <a href="https://www.f2er.com/tag/logo/" target="_blank" class="keywords">logo</a>nUser(sUsername,sDomain,sPassword,<a href="https://www.f2er.com/tag/logo/" target="_blank" class="keywords">logo</a>N32_<a href="https://www.f2er.com/tag/logo/" target="_blank" class="keywords">logo</a>N_NEWCREDENTIALS,<a href="https://www.f2er.com/tag/logo/" target="_blank" class="keywords">logo</a>N32_PROVIDER_DEFAULT,ref pExistingTokenHandle);
    if (true == bImpersonated)
    {
      if (!ImpersonateLoggedOnUser(pExistingTokenHandle))
      {
        int nErrorCode = Marshal.GetLastWin32Error();
        throw new Exception("ImpersonateLoggedOnUser error;Code=" + nErrorCode);
      }
    }
    else
    {
      int nErrorCode = Marshal.GetLastWin32Error();
      throw new Exception("<a href="https://www.f2er.com/tag/logo/" target="_blank" class="keywords">logo</a>nUser error;Code=" + nErrorCode);
    }
  }
  finally
  {
    // close handle(s)
    if (pExistingTokenHandle != IntPtr.Zero)
      CloseHandle(pExistingTokenHandle);
    if (pDuplicateTokenHandle != IntPtr.Zero)
      CloseHandle(pDuplicateTokenHandle);
  }
}
protected virtual void Dispose(bool disposing)
{
  if (!disposed)
  {
    RevertToSelf();
    disposed = true;
  }
}
public void Dispose()
{
  Dispose(true);
}

}

第二个参数是域名,有域名的话写域名,没有域名写目标机器的IP就可以了

相关文章

Hessian开源的远程通讯,采用二进制 RPC的协议,基于 HTTP 传输。可以实现PHP调用Java,Python,C#等多语...
初识Mongodb的一些总结,在Mac Os X下真实搭建mongodb环境,以及分享个Mongodb管理工具,学习期间一些总结...
边看边操作,这样才能记得牢,实践是检验真理的唯一标准.光看不练假把式,光练不看傻把式,边看边练真把式....
在php中,结果输出一共有两种方式:echo和print,下面将对两种方式做一个比较。 echo与print的区别: (...
在安装好wampServer后,一直没有使用phpMyAdmin,今天用了一下,phpMyAdmin显示错误:The mbstring exte...
变量是用于存储数据的容器,与代数相似,可以给变量赋予某个确定的值(例如:$x=3)或者是赋予其它的变...