SessionSQLServerHandler.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. //
  2. // System.Web.SessionState.SessionSQLServerHandler
  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.IO;
  11. using System.Data;
  12. using System.Reflection;
  13. using System.Configuration;
  14. using System.Collections.Specialized;
  15. namespace System.Web.SessionState {
  16. internal class SessionSQLServerHandler : ISessionHandler
  17. {
  18. const int DefTimeout = 600;
  19. private Type cncType = null;
  20. private IDbConnection cnc = null;
  21. private SessionConfig config;
  22. public void Dispose ()
  23. {
  24. if (cnc != null) {
  25. cnc.Close ();
  26. cnc = null;
  27. }
  28. }
  29. public void Init (HttpApplication context, SessionConfig config)
  30. {
  31. string connectionTypeName;
  32. string providerAssemblyName;
  33. string cncString;
  34. this.config = config;
  35. GetConnectionData (out providerAssemblyName, out connectionTypeName, out cncString);
  36. if (cncType == null) {
  37. Assembly dbAssembly = Assembly.Load (providerAssemblyName);
  38. cncType = dbAssembly.GetType (connectionTypeName, true);
  39. if (!typeof (IDbConnection).IsAssignableFrom (cncType))
  40. throw new ApplicationException ("The type '" + cncType +
  41. "' does not implement IDB Connection.\n" +
  42. "Check 'DbConnectionType' in server.exe.config.");
  43. }
  44. cnc = (IDbConnection) Activator.CreateInstance (cncType);
  45. cnc.ConnectionString = cncString;
  46. try {
  47. cnc.Open ();
  48. } catch (Exception exc) {
  49. cnc = null;
  50. throw exc;
  51. }
  52. }
  53. public void UpdateHandler (HttpContext context, SessionStateModule module)
  54. {
  55. if (context.Session == null)
  56. return;
  57. string id = context.Session.SessionID;
  58. SessionDictionary dict = context.Session.SessionDictionary;
  59. UpdateSession (id, dict);
  60. }
  61. public bool UpdateContext (HttpContext context, SessionStateModule module)
  62. {
  63. HttpSessionState session = null;
  64. string id = SessionId.Lookup (context.Request, config.CookieLess);
  65. if (id != null) {
  66. session = SelectSession (id, module.IsReadOnly);
  67. if (session != null) {
  68. context.SetSession (session);
  69. return false;
  70. }
  71. }
  72. id = SessionId.Create (module.Rng);
  73. session = new HttpSessionState (id, new SessionDictionary (),
  74. new HttpStaticObjectsCollection (), config.Timeout, true,
  75. config.CookieLess, SessionStateMode.SQLServer, module.IsReadOnly);
  76. InsertSession (session, config.Timeout);
  77. context.SetSession (session);
  78. return true;
  79. }
  80. private void GetConnectionData (out string providerAssembly,
  81. out string cncTypeName, out string cncString)
  82. {
  83. providerAssembly = null;
  84. cncTypeName = null;
  85. cncString = null;
  86. NameValueCollection config = ConfigurationSettings.AppSettings as NameValueCollection;
  87. if (config != null) {
  88. foreach (string s in config.Keys) {
  89. if (0 == String.Compare ("StateDBProviderAssembly", s, true)) {
  90. providerAssembly = config [s];
  91. } else if (0 == String.Compare ("StateDBConnectionType", s, true)) {
  92. cncTypeName = config [s];
  93. }
  94. }
  95. }
  96. cncString = this.config.SqlConnectionString;
  97. if (providerAssembly == null || providerAssembly == String.Empty)
  98. providerAssembly = "Npgsql.dll";
  99. if (cncTypeName == null || cncTypeName == String.Empty)
  100. cncTypeName = "Npgsql.NpgsqlConnection";
  101. if (cncString == null || cncString == String.Empty)
  102. cncString = "SERVER=127.0.0.1;USER ID=monostate;PASSWORD=monostate;dbname=monostate";
  103. }
  104. private HttpSessionState SelectSession (string id, bool read_only)
  105. {
  106. HttpSessionState session = null;
  107. IDbCommand command = cnc.CreateCommand();
  108. IDataReader reader;
  109. string select = "SELECT * from aspstatetempsessions WHERE SessionID = :SessionID";
  110. command.CommandText = select;
  111. command.Parameters.Add (CreateParam (command, DbType.String, ":SessionID", id));
  112. try {
  113. reader = command.ExecuteReader ();
  114. if (!reader.Read ())
  115. return null;
  116. SessionDictionary dict;
  117. HttpStaticObjectsCollection sobjs;
  118. dict = SessionDictionary.FromByteArray (ReadBytes (reader, reader.FieldCount-1));
  119. sobjs = HttpStaticObjectsCollection.FromByteArray (ReadBytes (reader, reader.FieldCount-2));
  120. session = new HttpSessionState (id, dict, sobjs, 100, false, config.CookieLess,
  121. SessionStateMode.SQLServer, read_only);
  122. return session;
  123. } catch {
  124. throw;
  125. }
  126. }
  127. private void InsertSession (HttpSessionState session, int timeout)
  128. {
  129. IDbCommand command = cnc.CreateCommand ();
  130. IDataParameterCollection param;
  131. string insert = "INSERT INTO ASPStateTempSessions VALUES " +
  132. "(:SessionID, :Created, :Expires, :Timeout, :StaticObjectsData, :SessionData)";
  133. command.CommandText = insert;
  134. param = command.Parameters;
  135. param.Add (CreateParam (command, DbType.String, ":SessionID", session.SessionID));
  136. param.Add (CreateParam (command, DbType.DateTime, ":Created", DateTime.Now));
  137. param.Add (CreateParam (command, DbType.DateTime, ":Expires", Tommorow ()));
  138. param.Add (CreateParam (command, DbType.Int32, ":Timeout", timeout));
  139. param.Add (CreateParam (command, DbType.Binary, ":StaticObjectsData",
  140. session.StaticObjects.ToByteArray ()));
  141. param.Add (CreateParam (command, DbType.Binary, ":SessionData",
  142. session.SessionDictionary.ToByteArray ()));
  143. command.ExecuteNonQuery ();
  144. }
  145. private void UpdateSession (string id, SessionDictionary dict)
  146. {
  147. IDbCommand command = cnc.CreateCommand ();
  148. IDataParameterCollection param;
  149. string update = "UPDATE ASPStateTempSessions SET " +
  150. "SessionData = :SessionData WHERE SessionId = :SessionID";
  151. command.CommandText = update;
  152. param = command.Parameters;
  153. param.Add (CreateParam (command, DbType.String, ":SessionID", id));
  154. param.Add (CreateParam (command, DbType.Binary, ":SessionData",
  155. dict.ToByteArray ()));
  156. command.ExecuteNonQuery ();
  157. }
  158. private IDataParameter CreateParam (IDbCommand command, DbType type,
  159. string name, object value)
  160. {
  161. IDataParameter result = command.CreateParameter ();
  162. result.DbType = type;
  163. result.ParameterName = name;
  164. result.Value = value;
  165. return result;
  166. }
  167. private DateTime Tommorow ()
  168. {
  169. return DateTime.Now.AddDays (1);
  170. }
  171. private byte [] ReadBytes (IDataReader reader, int index)
  172. {
  173. int len = (int) reader.GetBytes (reader.FieldCount-1, 0, null, 0, 0);
  174. byte [] data = new byte [len];
  175. reader.GetBytes (index, 0, data, 0, len);
  176. return data;
  177. }
  178. }
  179. }