How to

convert SecureString to string

Published: 30. September 2011 | Updated: 30. September 2011
License: Microsoft Public License (MS-PL)
Categories: Converting, Security
Tags: C# Security Text Windows
Was this snippet helpful for you? YESYES / NONO

Import namespaces

using System.Runtime.InteropServices;
using System.Security;

Code

var password = new SecureString();
password.AppendChar('p');
password.AppendChar('a');
password.AppendChar('s');
password.AppendChar('s');
password.AppendChar('w');
password.AppendChar('o');
password.AppendChar('r');
password.AppendChar('d');

IntPtr bstr = Marshal.SecureStringToBSTR(password);

try
{
    Console.WriteLine(Marshal.PtrToStringBSTR(bstr));
}
finally
{
    Marshal.FreeBSTR(bstr);
}
Console Output:
password
Send us feedback about this snippet »



Related Snippets: