Skip to content

Instantly share code, notes, and snippets.

@ti-ka
Created September 11, 2018 15:30
Show Gist options
  • Save ti-ka/72350c4cd10306cd2c7eee57442a6565 to your computer and use it in GitHub Desktop.
Save ti-ka/72350c4cd10306cd2c7eee57442a6565 to your computer and use it in GitHub Desktop.
Generate Token C#
var bytes = new byte[40]; // byte size
using (var crypto = new RNGCryptoServiceProvider())
crypto.GetBytes(bytes);
var base64 = Convert.ToBase64String(bytes);
Console.WriteLine(base64);
// Prepend with a Guid:
var result = Guid.NewGuid().ToString("N") + Convert.ToBase64String(bytes);
Console.WriteLine(result);
// Alphanumeric string:
result = Regex.Replace(result,"[^A-Za-z0-9]","");
Console.WriteLine(result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment