asp.net訪問網(wǎng)絡(luò)路徑方法(模擬用戶登錄)
更新時(shí)間:2014年08月17日 10:22:06 投稿:mdxy-dxy
這篇文章主要介紹了asp.net訪問網(wǎng)絡(luò)路徑方法,其實(shí)就是模擬用戶登錄,需要的朋友可以參考下
核心代碼:
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); } }
第二個(gè)參數(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功能實(shí)現(xiàn)代碼
- asp.net下URL網(wǎng)址重寫成.html格式、RSS、OPML的知識(shí)總結(jié)
- 用 Asp.Net 建立一個(gè)在線 RSS 新聞聚合器的方法
- 一個(gè)ASP.NET的MYSQL的數(shù)據(jù)庫操作類自己封裝的
- asp.net截屏功能實(shí)現(xiàn)截取web頁面
- Asp.net中將Word文件轉(zhuǎn)換成HTML的方法
- ASP.NET自動(dòng)為URL加上超鏈接的代碼
- 服務(wù)器安全狗導(dǎo)致ASP.NET網(wǎng)站運(yùn)行出錯(cuò)的一個(gè)案例
- Asp.Net中的字符串和HTML十進(jìn)制編碼轉(zhuǎn)換實(shí)現(xiàn)代碼
- ASP.NET讀取RSS的方法
相關(guān)文章
.NET?Core利用BsonDocumentProjectionDefinition和Lookup進(jìn)行?join?
這篇文章主要介紹了.NET?Core利用BsonDocumentProjectionDefinition和Lookup進(jìn)行join關(guān)聯(lián)查詢,這里主要介紹一下查詢角色的所有用戶的例子,文章結(jié)合實(shí)例代碼給大家詳細(xì)講解,需要的朋友可以參考下2022-10-10swfupload ajax無刷新上傳圖片實(shí)例代碼
在這里上傳圖片就需要用到ajax無刷新上傳圖片,這里面包含的東西不是一點(diǎn)半點(diǎn)。這里用到的是一個(gè)插件swfupload實(shí)現(xiàn)無刷新上傳圖片,感興趣的朋友可以參考下哈2013-06-06asp.net ajaxControlToolkit FilteredTextBoxExtender的簡單用法
最近寫的東西驗(yàn)證比較多,尤其是數(shù)字驗(yàn)證,無意中發(fā)現(xiàn)這個(gè)控件,有點(diǎn)兒意思。記錄一下2008-11-11Asp.Net網(wǎng)站優(yōu)化系列之?dāng)?shù)據(jù)庫的優(yōu)化措施與索引優(yōu)化方法
索引的作用就類似于書的目錄,書的目錄會(huì)按照章節(jié)的順序排列,會(huì)指想某一張的位置。這樣如果在一本數(shù)百頁的書里面查找某個(gè)章節(jié)位置的時(shí)候,我們就可以只掃描書的目錄,掃描的范圍縮小了n倍,查詢的效率自然就提高了。2010-06-06asp.net Cookie跨域、虛擬目錄等設(shè)置方法
Cookie跨域、虛擬目錄等設(shè)置方法,需要的朋友可以參考下。2009-11-11