如何将xml中的字符串用于c#中的字符串变量?

如何将xml中的字符串用于c#中的字符串变量?,c#,asp.net,xml,powershell,securestring,C#,Asp.net,Xml,Powershell,Securestring,嗨,我有一个包含两个值的xml文件 第一个值是Powershell的用户名 第二个值是powershell的密码作为securestring 现在我想读取这些值,并为变量string ps_user和SecureString ps_password设置这些值 我现在的问题是如何使用SecureString值 这里是我的xml: <?xml version="1.0" encoding="iso-8859-1"?> <Credential> <User valu

嗨,我有一个包含两个值的xml文件

第一个值是Powershell的用户名 第二个值是powershell的密码作为securestring

现在我想读取这些值,并为变量string ps_user和SecureString ps_password设置这些值

我现在的问题是如何使用SecureString值

这里是我的xml:

<?xml version="1.0" encoding="iso-8859-1"?>

<Credential>
  <User value="tarasov" />
  <SecurePassword value="0d08c9ddf0004800000a0000340b62f9d614" />
</Credential>
> 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 :((
示例:

<?xml version="1.0" encoding="iso-8859-1"?>

<Credential>
  <User value="tarasov" />
  <SecurePassword value="0d08c9ddf0004800000a0000340b62f9d614" />
</Credential>
> 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 :((

如何执行此操作?

是因为GetPowershellCredentials返回字符串。这无法自动转换。如果您需要安全字符串,可以使用以下内容:

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"));

但是我的xml中的值是securestring(Fehler bei SSPI Aufruf,siehe interne Ausnahme。)在英语中->错误由SSPI调用,看看intern执行选项…我认为安全字符串不是用来保存到文件中并从那里读取的。可能尝试使用其他加密并将加密密码保存在XML中。然后,您只需要将解密后的密码转换为安全字符串。试试看。万岁,格吕克·达贝!