[英]How to open connection to local network path protected by password in smart way? (Whith C#)
我開發的程序必須將一些數據寫入文件,這些數據存儲在網絡計算機中並受密碼保護。 現在,我正在這樣做-用cmd打開連接,然后寫入數據。
static bool ConnectToSrv()
{
String myUser = "domain.local\\user";
String myPass = "pwd";
String cmdString = "net use \\\\otherPC\\folder\\ /user:" + myUser + " " + myPass;
try
{
ManagementClass processClass = new ManagementClass("Win32_Process");
object[] methodArgs = { cmdString, null, null, 0 };
object result = processClass.InvokeMethod("Create", methodArgs);
return true;
}
catch (System.Exception error)
{
return false;
}
}
public void writeDate(string data){ }
我相信必須以更好的方式。 我的意思是.NET方式。 有人知道怎么做嗎? :)
謝謝
您應該在配置文件中存儲哈希密碼和加鹽密碼,並在配置代碼中使用它之前從config中讀取值。 關於如何執行此操作的參考文獻很多,因此只需進行搜索即可。
嘗試模擬。 您需要訪問此api
// Using this api to get an accessToken of specific Windows User by its user name and password
[DllImport("advapi32.dll",CharSet=CharSet.Unicode,SetLastError=true)]
static public extern bool LogonUser(string userName,string domain,string passWord,int logonType,int logonProvider,ref IntPtr accessToken);
然后使用用戶名/密碼模擬用戶所需的憑據並訪問網絡。
private const int LOGON_TYPE_INTERACTIVE = 2;
private const int LOGON_TYPE_PROVIDER_DEFAULT = 0;
IntPtr accessToken = IntPtr.Zero;
if (LogonUser(userName, domain, password, LOGON_TYPE_INTERACTIVE, LOGON_TYPE_PROVIDER_DEFAULT, ref accessToken))
{
WindowsIdentity identity = new WindowsIdentity(accessToken);
WindowsImpersonationContext context = identity.Impersonate();
// Access network here ....
}
感謝並從此博客中獲取了代碼
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.