How to convert a string to securestring explicitly
The simplest approach is to iterate over the source string and append one character at a time to the secure string, like so:
var secure = new SecureString();
foreach (char c in textbox1.Text)
{
secure.AppendChar(c);
}
Invent once and reuse lots. Create a simple extension method to extend the string base class and store it some static utilities class somewhere
using System.Security;
/// <summary>
/// Returns a Secure string from the source string
/// </summary>
/// <param name="Source"></param>
/// <returns></returns>
public static SecureString ToSecureString(this string source)
{
if (string.IsNullOrWhiteSpace(source))
return null;
else
{
SecureString result = new SecureString();
foreach (char c in source.ToCharArray())
result.AppendChar(c);
return result;
}
}
and then call as follows:
textbox1.Text.ToSecureString();