Привет, У меня есть xml файл с двумя значениями.
Первое значение - это имя пользователя для Powershell. Второе значение - пароль как securestring для powershell
Теперь я хочу прочитать эти значения и установить это для переменных string ps_user и SecureString ps_password
Теперь мой вопрос заключается в том, как я могу использовать значение SecureString.
здесь мой xml:
<?xml version="1.0" encoding="iso-8859-1"?>
<Credential>
<User value="tarasov" />
<SecurePassword value="0d08c9ddf0004800000a0000340b62f9d614" />
</Credential>
здесь мой код С#:
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 :((
Как я могу это сделать?
Ist, потому что ваш GetPowershellCredentials возвращает строку. Это невозможно преобразовать автоматически. Если вам нужна Secure String, вы можете использовать что-то вроде этого:
public static SecureString ToSecureString(string source)
{
if (string.IsNullOrWhiteSpace(source))
return null;
else
{
SecureString result = new SecureString();
foreach (char c in source.ToCharArray())
result.AppendChar(c);
return result;
}
}
и это:
SecureString ps_password = ToSecureString(GetPowershellCredentials(path, "SecurePassword"));