asp.net訪問網(wǎng)絡路徑方法(模擬用戶登錄)
更新時間:2014年08月17日 10:22:06 投稿:mdxy-dxy
這篇文章主要介紹了asp.net訪問網(wǎng)絡路徑方法,其實就是模擬用戶登錄,需要的朋友可以參考下
核心代碼:
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用?機ú器÷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ù)是域名,有域名的話寫域名,沒有域名寫目標機器的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加上超鏈接的代碼
- 服務器安全狗導致ASP.NET網(wǎng)站運行出錯的一個案例
- Asp.Net中的字符串和HTML十進制編碼轉(zhuǎn)換實現(xiàn)代碼
- ASP.NET讀取RSS的方法
相關文章
.NET?Core利用BsonDocumentProjectionDefinition和Lookup進行?join?
這篇文章主要介紹了.NET?Core利用BsonDocumentProjectionDefinition和Lookup進行join關聯(lián)查詢,這里主要介紹一下查詢角色的所有用戶的例子,文章結(jié)合實例代碼給大家詳細講解,需要的朋友可以參考下2022-10-10asp.net ajaxControlToolkit FilteredTextBoxExtender的簡單用法
最近寫的東西驗證比較多,尤其是數(shù)字驗證,無意中發(fā)現(xiàn)這個控件,有點兒意思。記錄一下2008-11-11Asp.Net網(wǎng)站優(yōu)化系列之數(shù)據(jù)庫的優(yōu)化措施與索引優(yōu)化方法
索引的作用就類似于書的目錄,書的目錄會按照章節(jié)的順序排列,會指想某一張的位置。這樣如果在一本數(shù)百頁的書里面查找某個章節(jié)位置的時候,我們就可以只掃描書的目錄,掃描的范圍縮小了n倍,查詢的效率自然就提高了。2010-06-06