FormsAuthentication.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. //
  2. // System.Web.Security.FormsAuthentication
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (C) 2002 Ximian, Inc (http://www.ximian.com)
  8. //
  9. using System;
  10. using System.Collections;
  11. using System.Security.Cryptography;
  12. using System.Text;
  13. using System.Web;
  14. using System.Web.Configuration;
  15. using System.Web.Util;
  16. namespace System.Web.Security
  17. {
  18. public sealed class FormsAuthentication
  19. {
  20. static string authConfigPath = "system.web/authentication";
  21. static bool initialized;
  22. static string cookieName;
  23. static string cookiePath;
  24. static int timeout;
  25. static FormsProtectionEnum protection;
  26. public static bool Authenticate (string name, string password)
  27. {
  28. if (name == null || password == null)
  29. return false;
  30. Initialize ();
  31. HttpContext context = HttpContext.Current;
  32. AuthConfig config = context.GetConfig (authConfigPath) as AuthConfig;
  33. Hashtable users = config.CredentialUsers;
  34. string stored = users [name] as string;
  35. if (stored == null)
  36. return false;
  37. switch (config.PasswordFormat) {
  38. case FormsAuthPasswordFormat.Clear:
  39. /* Do nothing */
  40. break;
  41. case FormsAuthPasswordFormat.MD5:
  42. stored = HashPasswordForStoringInConfigFile (stored, "MD5");
  43. break;
  44. case FormsAuthPasswordFormat.SHA1:
  45. stored = HashPasswordForStoringInConfigFile (stored, "SHA1");
  46. break;
  47. }
  48. return (password == stored);
  49. }
  50. public static FormsAuthenticationTicket Decrypt (string encryptedTicket)
  51. {
  52. if (encryptedTicket == null || encryptedTicket == String.Empty)
  53. throw new ArgumentException ("Invalid encrypted ticket", "encryptedTicket");
  54. Initialize ();
  55. byte [] bytes = MachineKeyConfigHandler.GetBytes (encryptedTicket, encryptedTicket.Length);
  56. //TODO: decrypt
  57. string decrypted = WebEncoding.Encoding.GetString (bytes);
  58. FormsAuthenticationTicket ticket = null;
  59. try {
  60. string [] values = decrypted.Split ((char) 1, (char) 2, (char) 3, (char) 4, (char) 5, (char) 6, (char) 7);
  61. if (values.Length != 8)
  62. throw new Exception (values.Length + " " + encryptedTicket);
  63. ticket = new FormsAuthenticationTicket (Int32.Parse (values [0]),
  64. values [1],
  65. new DateTime (Int64.Parse (values [2])),
  66. new DateTime (Int64.Parse (values [3])),
  67. (values [4] == "1"),
  68. values [5],
  69. values [6]);
  70. } catch (Exception e) {
  71. throw new ArgumentException ("Invalid encrypted ticket", "encryptedTicket", e);
  72. }
  73. return ticket;
  74. }
  75. public static string Encrypt (FormsAuthenticationTicket ticket)
  76. {
  77. if (ticket == null)
  78. throw new ArgumentNullException ("ticket");
  79. Initialize ();
  80. StringBuilder allTicket = new StringBuilder ();
  81. allTicket.Append (ticket.Version);
  82. allTicket.Append ('\u0001');
  83. allTicket.Append (ticket.Name);
  84. allTicket.Append ('\u0002');
  85. allTicket.Append (ticket.IssueDate.Ticks);
  86. allTicket.Append ('\u0003');
  87. allTicket.Append (ticket.Expiration.Ticks);
  88. allTicket.Append ('\u0004');
  89. allTicket.Append (ticket.IsPersistent ? '1' : '0');
  90. allTicket.Append ('\u0005');
  91. allTicket.Append (ticket.UserData);
  92. allTicket.Append ('\u0006');
  93. allTicket.Append (ticket.CookiePath);
  94. allTicket.Append ('\u0007');
  95. //if (protection == FormsProtectionEnum.None)
  96. return GetHexString (allTicket.ToString ());
  97. //TODO: encrypt and validate
  98. }
  99. [MonoTODO]
  100. public static HttpCookie GetAuthCookie (string userName, bool createPersistentCookie)
  101. {
  102. throw new NotImplementedException ();
  103. }
  104. [MonoTODO]
  105. public static HttpCookie GetAuthCookie (string userName, bool createPersistentCookie, string strCookiePath)
  106. {
  107. throw new NotImplementedException ();
  108. }
  109. [MonoTODO]
  110. public static string GetRedirectUrl (string userName, bool createPersistentCookie)
  111. {
  112. throw new NotImplementedException ();
  113. }
  114. static string GetHexString (string str)
  115. {
  116. return GetHexString (WebEncoding.Encoding.GetBytes (str));
  117. }
  118. static string GetHexString (byte [] bytes)
  119. {
  120. StringBuilder result = new StringBuilder (bytes.Length * 2);
  121. foreach (byte b in bytes)
  122. result.AppendFormat ("{0:x2}", (int) b);
  123. return result.ToString ();
  124. }
  125. public static string HashPasswordForStoringInConfigFile (string password, string passwordFormat)
  126. {
  127. if (password == null)
  128. throw new ArgumentNullException ("password");
  129. if (passwordFormat == null)
  130. throw new ArgumentNullException ("passwordFormat");
  131. byte [] bytes;
  132. if (String.Compare (passwordFormat, "MD5", true) == 0) {
  133. bytes = MD5.Create ().ComputeHash (WebEncoding.Encoding.GetBytes (password));
  134. } else if (String.Compare (passwordFormat, "SHA1", true) == 0) {
  135. bytes = SHA1.Create ().ComputeHash (WebEncoding.Encoding.GetBytes (password));
  136. } else {
  137. throw new ArgumentException ("The format must be either MD5 or SHA1", "passwordFormat");
  138. }
  139. return GetHexString (bytes);
  140. }
  141. public static void Initialize ()
  142. {
  143. if (initialized)
  144. return;
  145. lock (typeof (FormsAuthentication)) {
  146. if (initialized)
  147. return;
  148. HttpContext context = HttpContext.Current;
  149. AuthConfig authConfig = context.GetConfig (authConfigPath) as AuthConfig;
  150. if (authConfig != null) {
  151. cookieName = authConfig.CookieName;
  152. timeout = authConfig.Timeout;
  153. cookiePath = authConfig.CookiePath;
  154. protection = authConfig.Protection;
  155. } else {
  156. cookieName = ".MONOAUTH";
  157. timeout = 30;
  158. cookiePath = "/";
  159. protection = FormsProtectionEnum.All;
  160. }
  161. initialized = true;
  162. }
  163. }
  164. [MonoTODO]
  165. public static void RedirectFromLoginPage (string userName, bool createPersistentCookie)
  166. {
  167. throw new NotImplementedException ();
  168. }
  169. [MonoTODO]
  170. public static void RedirectFromLoginPage (string userName, bool createPersistentCookie, string strCookiePath)
  171. {
  172. throw new NotImplementedException ();
  173. }
  174. [MonoTODO]
  175. public static FormsAuthenticationTicket RenewTicketIfOld (FormsAuthenticationTicket tOld)
  176. {
  177. throw new NotImplementedException ();
  178. }
  179. [MonoTODO]
  180. public static void SetAuthCookie (string userName, bool createPersistentCookie)
  181. {
  182. throw new NotImplementedException ();
  183. }
  184. [MonoTODO]
  185. public static void SetAuthCookie (string userName, bool createPersistentCookie, string strCookiePath)
  186. {
  187. throw new NotImplementedException ();
  188. }
  189. [MonoTODO]
  190. public static void SignOut ()
  191. {
  192. throw new NotImplementedException ();
  193. }
  194. [MonoTODO]
  195. public static string FormsCookieName
  196. {
  197. get {
  198. throw new NotImplementedException ();
  199. }
  200. }
  201. [MonoTODO]
  202. public static string FormsCookiePath
  203. {
  204. get {
  205. throw new NotImplementedException ();
  206. }
  207. }
  208. }
  209. }