Skip to content

Instantly share code, notes, and snippets.

@jasongaylord
Last active January 16, 2016 02:06
Show Gist options
  • Save jasongaylord/49a17a9bc49e38d7ca71 to your computer and use it in GitHub Desktop.
Save jasongaylord/49a17a9bc49e38d7ca71 to your computer and use it in GitHub Desktop.
A Helper method to convert a string value to a SecureString value. This is most useful when needing to pass a sensitive value, such as a social security number or password, around as a string on the server side. The SecureString class has a Dispose method for GC.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security;
using System.Web;
namespace Gaylord.Helpers
{
public static class SecureStringHelper
{
public static SecureString ToSecureString(this string value)
{
var rtn = new SecureString();
if (value.Length > 0)
foreach (var c in value.ToCharArray())
rtn.AppendChar(c);
return rtn;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment