.net5訪問共享目錄
windows平臺訪問遠程服務器共享目錄下的access數(shù)據(jù)庫文件
public class SharedDirectoryManager { // logon types private const int Logon32_Logon_Interactive = 2; private const int Logon32_Logon_Network = 3; private const int Logon32_Logon_New_Credentials = 9; // logon providers private const int Logon32_Provider_Default = 0; private const int Logon32_Provider_Winnt50 = 3; private const int Logon32_Provider_Winnt40 = 2; private const int Logon32_Provider_Winnt35 = 1; private WindowsImpersonationContext impersonationContext; [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern int LogonUser(string lpszUserName, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken); [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern int DuplicateToken(IntPtr hToken, int impersonationLevel, ref IntPtr hNewToken); [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern bool RevertToSelf(); [DllImport("kernel32.dll", CharSet = CharSet.Auto)] private static extern bool CloseHandle(IntPtr handle); public bool ImpersonateValidUser(string userName, string domain, string password) { var token = IntPtr.Zero; var tokenDuplicate = IntPtr.Zero; if (RevertToSelf()) { // 這里使用LOGON32_LOGON_NEW_CREDENTIALS來訪問遠程資源。 // 如果要(通過模擬用戶獲得權(quán)限)實現(xiàn)服務器程序,訪問本地授權(quán)數(shù)據(jù)庫可 // 以用LOGON32_LOGON_INTERACTIVE if (LogonUser(userName, domain, password, Logon32_Logon_New_Credentials, Logon32_Provider_Default, ref token) != 0) { if (DuplicateToken(token, 2, ref tokenDuplicate) != 0) { var tempWindowsIdentity = new WindowsIdentity(tokenDuplicate); this.impersonationContext = tempWindowsIdentity.Impersonate(); if (this.impersonationContext != null) { AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal); CloseHandle(token); CloseHandle(tokenDuplicate); return true; } } } } if (token != IntPtr.Zero) CloseHandle(token); if (tokenDuplicate != IntPtr.Zero) CloseHandle(tokenDuplicate); return false; } public void UndoImpersonation() { this.impersonationContext.Undo(); } }
調(diào)ImpersonateValidUser成功后,按照本地目錄可正常訪問
如果是訪問共享的access數(shù)據(jù)庫文件
指定連接字符串:Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\192.168.137.200\ShareFolder\test.mdb;OLE DB Services=-4;
為共享目錄文件地址即可
如果不加OLE DB Services=-4;,可能會導致web中訪問數(shù)據(jù)庫時發(fā)生:
Microsoft.Jet.OLEDB.4.0 提供程序不支持 ITransactionLocal 接口。本地事務不可用于當前提供程序 的異常
跨平臺(.net 5)共享目錄訪問解決方案
通過shell命令實現(xiàn): net use \\192.168.3.163\software /User:administrator 123456 /PERSISTENT:YES
public class RemoteFileOperate { /// <summary> /// 遠程訪問目錄 /// </summary> public string remotePath { get; set; } /// <summary> /// 用戶名 /// </summary> public string userName { get; set; } /// <summary> /// 密碼 /// </summary> public string passWord { get; set; } public RemoteFileOperate(string remotePath) { this.remotePath = remotePath; } public RemoteFileOperate(string remotePath, string userName, string passWord) { this.remotePath = remotePath; this.userName = userName; this.passWord = passWord; } /// <summary> /// 檢測遠程訪問連接狀態(tài) /// </summary> /// <param name="path"></param> /// <param name="isNeedPersistent"></param> /// <returns></returns> public bool CheckConnectState() { bool Flag = false; Process proc = new Process(); try { proc.StartInfo.FileName = "cmd.exe"; proc.StartInfo.UseShellExecute = false; proc.StartInfo.RedirectStandardInput = true; proc.StartInfo.RedirectStandardOutput = true; proc.StartInfo.RedirectStandardError = true; proc.StartInfo.CreateNoWindow = true; proc.Start(); string dosLine = string.Empty; string dosLineClean = @"net use * /del /y"; if (!string.IsNullOrEmpty(userName) || !string.IsNullOrEmpty(passWord)) { dosLine = @"net use " + remotePath + " /User:" + userName + " " + passWord + " /PERSISTENT:YES"; } else { dosLine = @"net use " + remotePath + " /PERSISTENT:NO"; } proc.StandardInput.WriteLine(dosLineClean); proc.StandardInput.WriteLine(dosLine); proc.StandardInput.WriteLine("exit"); while (!proc.HasExited) { proc.WaitForExit(1000); } string errormsg = proc.StandardError.ReadToEnd(); proc.StandardError.Close(); if (string.IsNullOrEmpty(errormsg)) { Flag = true; } } catch (Exception ex) { } finally { proc.Close(); proc.Dispose(); } return Flag; } }
Newd
版權(quán)聲明
作者:扶我起來我還要敲
地址:http://www.rzrgm.cn/Newd/p/14568946.html
? Newd 尊重知識產(chǎn)權(quán),引用請注出處
廣告位
(虛位以待,如有需要請私信)
浙公網(wǎng)安備 33010602011771號