SessionId.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //
  2. // System.Web.SessionState.SessionId
  3. //
  4. // Author(s):
  5. // Jackson Harper ([email protected])
  6. //
  7. // (C) 2003 Novell, Inc (http://www.novell.com), All rights reserved
  8. //
  9. using System;
  10. using System.Text;
  11. using System.Security.Cryptography;
  12. namespace System.Web.SessionState {
  13. internal class SessionId {
  14. private static char [] allowed = { '0', '1', '2', '3', '4', '5',
  15. '6', '7', '8', '9', 'A', 'B',
  16. 'C', 'D', 'E', 'F' };
  17. internal static readonly int IdLength = 30;
  18. private static readonly int half_len = 15;
  19. internal static string Create (RandomNumberGenerator rng)
  20. {
  21. if (rng == null)
  22. throw new ArgumentNullException ("rng");
  23. byte[] key = new byte [half_len];
  24. rng.GetBytes (key);
  25. return Encode (key);
  26. }
  27. internal static string Encode (byte[] key)
  28. {
  29. if (key == null)
  30. throw new ArgumentNullException ("key");
  31. if (key.Length != half_len)
  32. throw new ArgumentException ("key must be 15 bytes long.");
  33. // Just a standard hex conversion
  34. char[] res = new char [IdLength];
  35. for (int i=0; i < half_len; i++) {
  36. int b = key [i];
  37. res [i * 2] = allowed [b >> 4];
  38. res [(i * 2) + 1] = allowed [b & 0xF];
  39. }
  40. return new String (res);
  41. }
  42. internal static string Lookup (HttpRequest request, bool cookieless)
  43. {
  44. if (cookieless)
  45. return (string) request.Headers [SessionStateModule.HeaderName];
  46. else if (request.Cookies [SessionStateModule.CookieName] != null)
  47. return request.Cookies [SessionStateModule.CookieName].Value;
  48. return null;
  49. }
  50. }
  51. }