时间:2021-05-02
核心代码:
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 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); } }第二个参数是域名,有域名的话写域名,没有域名写目标机器的IP就可以了
? 1 2 3 4 using (IdentityScope c = new IdentityScope("administrator", "192.168.0.1", "11111")) { string[] filelist = System.IO.Directory.GetDirectories(@"\\192.168.0.1\folderName"); }声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
在你将现有的用户登录(SignIn)站点从ASP.NET迁移至ASP.NETCore时,你将面临这样一个问题——如何让ASP.NET与ASP.NETCore用户
本文实例讲述了PHP模拟asp.net的StringBuilder类实现方法。分享给大家供大家参考。具体如下:在asp.net开发开发环境中,有一个String
本文实例讲述了PHP模拟asp中response类的方法。分享给大家供大家参考。具体如下:习惯了asp或是asp.net开发的人,他们会经常用到response
前言作为.NET程序员,痛苦之一是自从ASP.NET诞生之日起直到最新的ASP.NETCore都无法直接实现跨站登录重定向(比如访问https://q.cnbl
本文实例讲述了ASP.NET动态添加用户控件的方法。分享给大家供大家参考。具体实现方法如下:为了让用户控件能ASP.NET页面实现动态添加,首先写一个接口IGe