首页 > 解决方案 > 如何使用 C# 保存网络凭据

问题描述

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

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) 需要它。

谁能给我一个解决方案...?

仅供参考,我自己将 192.168.7.202 的凭据添加到红色框中,使 Directory.GetAccessControl 运行良好。希望我可以在 C# 脚本上做到这一点..

在此处输入图像描述

标签: c#networkingnetworkcredentials

解决方案


推荐阅读