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

前端之家收集整理的这篇文章主要介绍了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="/tag/logo/" target="_blank" class="keywords">logo</a>N32_PROVIDER_DEFAULT = 0;
const int <a href="/tag/logo/" target="_blank" class="keywords">logo</a>N32_<a href="/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="/tag/logo/" target="_blank" class="keywords">logo</a>nUser(sUsername,sDomain,sPassword,<a href="/tag/logo/" target="_blank" class="keywords">logo</a>N32_<a href="/tag/logo/" target="_blank" class="keywords">logo</a>N_NEWCREDENTIALS,<a href="/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="/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就可以了

猜你在找的PHP相关文章