嗨,我有一个包含两个值的 xml 文件。
第一个值是 Powershell 的用户名第二个值是密码作为 powershell 的安全字符串
现在我想读取这些值并将其设置为变量字符串 ps_user 和 SecureString ps_password
我现在的问题是如何使用 SecureString 值。
这是我的xml:
<?xml version="1.0" encoding="iso-8859-1"?>
<Credential>
<User value="tarasov" />
<SecurePassword value="0d08c9ddf0004800000a0000340b62f9d614" />
</Credential>
这是我的 C# 代码:
private string GetPowershellCredentials(string path, string attribute)
{
XDocument document;
string value = string.Empty;
try
{
document = XDocument.Load(path);
value = document.Element("Credential").Element(attribute).Attribute("value").Value;
return value;
}
catch (Exception)
{
return null;
}
finally
{
document = null;
}
}
例子:
> string path = Server.MapPath("~/App_Data/Powershell_credentials.xml");
> string ps_user = GetPowershellCredentials(path, "User"); // It works
> SecureString ps_password = GetPowershellCredentials(path,"SecurePassword"); // this not :((
我怎么能做到这一点?