Skip to content

Instantly share code, notes, and snippets.

@codenamejason
Last active August 29, 2015 14:25
Show Gist options
  • Select an option

  • Save codenamejason/40d84694fc0972a84b12 to your computer and use it in GitHub Desktop.

Select an option

Save codenamejason/40d84694fc0972a84b12 to your computer and use it in GitHub Desktop.
RandomOAuthStateGen
private static class RandomOAuthStateGenerator
{
private static RandomNumberGenerator _random = new RNGCryptoServiceProvider();
public static string Generate(int strengthInBits)
{
const int bitsPerByte = 8;
if (strengthInBits % bitsPerByte != 0)
{
throw new ArgumentException("strengthInBits must be evenly divisible by 8.", "strengthInBits");
}
int strengthInBytes = strengthInBits / bitsPerByte;
byte[] data = new byte[strengthInBytes];
_random.GetBytes(data);
return HttpServerUtility.UrlTokenEncode(data);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment