如何使用C#保存网络凭据

时间:2018-07-23 07:27:26

标签: c# networking networkcredentials

此代码需要为我的工作执行。

string filePath = "\\\\192.168.7.202\\Sample\sample.log"
var accessControlList = Directory.GetAccessControl( filePath );

不幸的是,如果没有网络凭证,此代码将无法工作。 因此必须在我使用的计算机上添加192.168.7.202的网络凭据。

谁能知道如何通过C#脚本添加Netowrk凭据, 的IP地址,用户名和密码?

这是我的代码,但是不起作用.....

string ipAddress = "192.168.7.202";
string username = "Administrator";
string password = "abcd";

NetworkCredential netCredential = new NetworkCredential(username, password, ipAddress);

CredentialCache theNetCache = new CredentialCache();
theNetCache.Add("192.168.7.202", 3389, "Basic", netCredential);

string filePath = "\\\\192.168.7.202\\Sample\sample.log"
var accessControlList = Directory.GetAccessControl( filePath ); **//Error occur without Credential**

可能不必永久保留凭据, 至少需要Directory.GetAccessControl(filePath)。

任何人都可以给我解决方案吗??

仅供参考,我自己在RED框中添加了192.168.7.202的凭据,这使Directory.GetAccessControl可以正常工作。 希望我可以在C#脚本上做到这一点。

enter image description here

0 个答案:

没有答案
相关问题