asp.net訪問網(wǎng)絡(luò)路徑方法(模擬用戶登錄)
更新時間:2014年08月17日 10:22:06 投稿:mdxy-dxy
這篇文章主要介紹了asp.net訪問網(wǎng)絡(luò)路徑方法,其實就是模擬用戶登錄,需要的朋友可以參考下
核心代碼:
public class IdentityScope : IDisposable
{
// obtains user token
[DllImport("advapi32.dll", SetLastError = true)]
static extern bool 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 LOGON32_PROVIDER_DEFAULT = 0;
const int LOGON32_LOGON_NEWCREDENTIALS = 9;//域ò控?中D的?需è要a用?:Interactive = 2
private bool disposed;
/// <summary>
/// 登?錄?
/// </summary>
/// <param name="sUsername">用?戶§名?</param>
/// <param name="sDomain">域ò名?,?如?果?不?在ú域ò中D就í使1用?機(jī)ú器÷IP地?址·</param>
/// <param name="sPassword">密ü碼?</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 = LogonUser(sUsername, sDomain, sPassword,LOGON32_LOGON_NEWCREDENTIALS, LOGON32_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("LogonUser 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);
}
}
第二個參數(shù)是域名,有域名的話寫域名,沒有域名寫目標(biāo)機(jī)器的IP就可以了
using (IdentityScope c = new IdentityScope("administrator", "192.168.0.1", "11111"))
{
string[] filelist = System.IO.Directory.GetDirectories(@"\\192.168.0.1\folderName");
}
您可能感興趣的文章:
- asp.net(c#) RSS功能實現(xiàn)代碼
- asp.net下URL網(wǎng)址重寫成.html格式、RSS、OPML的知識總結(jié)
- 用 Asp.Net 建立一個在線 RSS 新聞聚合器的方法
- 一個ASP.NET的MYSQL的數(shù)據(jù)庫操作類自己封裝的
- asp.net截屏功能實現(xiàn)截取web頁面
- Asp.net中將Word文件轉(zhuǎn)換成HTML的方法
- ASP.NET自動為URL加上超鏈接的代碼
- 服務(wù)器安全狗導(dǎo)致ASP.NET網(wǎng)站運行出錯的一個案例
- Asp.Net中的字符串和HTML十進(jìn)制編碼轉(zhuǎn)換實現(xiàn)代碼
- ASP.NET讀取RSS的方法
相關(guān)文章
詳解將ASP.NET Core應(yīng)用程序部署至生產(chǎn)環(huán)境中(CentOS7)
這篇文章主要介紹了詳解將ASP.NET Core應(yīng)用程序部署至生產(chǎn)環(huán)境中(CentOS7),具有一定的參考價值,有需要的可以了解一下。2016-12-12
使用grpcui測試ASP.NET core的gRPC服務(wù)
這篇文章介紹了使用grpcui測試ASP.NET core gRPC服務(wù)的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-07-07
.net中如何以純二進(jìn)制的形式在內(nèi)存中繪制一個對象
這篇文章主要介紹了如何以純二進(jìn)制的形式在內(nèi)存中繪制一個對象,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-07-07
簡單使用BackgroundWorker創(chuàng)建多個線程的教程
簡單使用BackgroundWorker創(chuàng)建多個線程的教程,需要的朋友可以參考一下2013-03-03
ASP.NET Core擴(kuò)展庫之日志功能的使用詳解
這篇文章主要介紹了ASP.NET Core擴(kuò)展庫之日志功能的使用詳解,幫助大家更好的理解和學(xué)習(xí)使用.NET技術(shù),感興趣的朋友可以了解下2021-03-03

