2 // System.Web.SessionState.SessionSQLServerHandler
5 // Jackson Harper (jackson@ximian.com)
7 // (C) 2003 Novell, Inc. (http://www.novell.com), All rights reserved
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Reflection;
35 using System.Configuration;
36 using System.Collections.Specialized;
38 namespace System.Web.SessionState {
40 internal class SessionSQLServerHandler : ISessionHandler
42 private static Type cncType = null;
43 private IDbConnection cnc = null;
44 private SessionConfig config;
46 public void Dispose ()
54 public void Init (SessionStateModule module, HttpApplication context, SessionConfig config)
56 string connectionTypeName;
57 string providerAssemblyName;
62 GetConnectionData (out providerAssemblyName, out connectionTypeName, out cncString);
63 if (cncType == null) {
64 Assembly dbAssembly = Assembly.Load (providerAssemblyName);
65 cncType = dbAssembly.GetType (connectionTypeName, true);
66 if (!typeof (IDbConnection).IsAssignableFrom (cncType))
67 throw new ApplicationException ("The type '" + cncType +
68 "' does not implement IDB Connection.\n" +
69 "Check 'DbConnectionType' in server.exe.config.");
72 cnc = (IDbConnection) Activator.CreateInstance (cncType);
73 cnc.ConnectionString = cncString;
76 } catch (Exception exc) {
82 public void UpdateHandler (HttpContext context, SessionStateModule module)
84 if (context.Session == null || context.Session.IsReadOnly)
87 string id = context.Session.SessionID;
88 SessionDictionary dict = context.Session.SessionDictionary;
90 UpdateSession (id, dict);
93 public HttpSessionState UpdateContext (HttpContext context, SessionStateModule module,
94 bool required, bool read_only, ref bool isNew)
99 HttpSessionState session = null;
100 string id = SessionId.Lookup (context.Request, config.CookieLess);
103 session = SelectSession (id, read_only);
108 id = SessionId.Create (module.Rng);
109 session = new HttpSessionState (id, new SessionDictionary (),
110 HttpApplicationFactory.ApplicationState.SessionObjects, config.Timeout,
111 true, config.CookieLess, SessionStateMode.SQLServer, read_only);
113 InsertSession (session, config.Timeout);
118 private void GetConnectionData (out string providerAssembly,
119 out string cncTypeName, out string cncString)
121 providerAssembly = null;
125 NameValueCollection config = ConfigurationSettings.AppSettings as NameValueCollection;
126 if (config != null) {
127 foreach (string s in config.Keys) {
128 if (0 == String.Compare ("StateDBProviderAssembly", s, true)) {
129 providerAssembly = config [s];
130 } else if (0 == String.Compare ("StateDBConnectionType", s, true)) {
131 cncTypeName = config [s];
136 cncString = this.config.SqlConnectionString;
138 if (providerAssembly == null || providerAssembly == String.Empty)
139 providerAssembly = "Npgsql.dll";
141 if (cncTypeName == null || cncTypeName == String.Empty)
142 cncTypeName = "Npgsql.NpgsqlConnection";
144 if (cncString == null || cncString == String.Empty)
145 cncString = "SERVER=127.0.0.1;USER ID=monostate;PASSWORD=monostate;dbname=monostate";
148 private HttpSessionState SelectSession (string id, bool read_only)
150 HttpSessionState session = null;
151 IDbCommand command = cnc.CreateCommand();
154 string select = "SELECT * from aspstatetempsessions WHERE SessionID = :SessionID";
156 command.CommandText = select;
158 command.Parameters.Add (CreateParam (command, DbType.String, ":SessionID", id));
161 reader = command.ExecuteReader ();
166 SessionDictionary dict;
167 HttpStaticObjectsCollection sobjs;
169 dict = SessionDictionary.FromByteArray (ReadBytes (reader, reader.FieldCount-1));
170 sobjs = HttpStaticObjectsCollection.FromByteArray (ReadBytes (reader, reader.FieldCount-2));
172 session = new HttpSessionState (id, dict, sobjs, 100, false, config.CookieLess,
173 SessionStateMode.SQLServer, read_only);
180 private void InsertSession (HttpSessionState session, int timeout)
182 IDbCommand command = cnc.CreateCommand ();
183 IDataParameterCollection param;
185 string insert = "INSERT INTO ASPStateTempSessions VALUES " +
186 "(:SessionID, :Created, :Expires, :Timeout, :StaticObjectsData, :SessionData)";
188 command.CommandText = insert;
190 param = command.Parameters;
191 param.Add (CreateParam (command, DbType.String, ":SessionID", session.SessionID));
192 param.Add (CreateParam (command, DbType.DateTime, ":Created", DateTime.Now));
193 param.Add (CreateParam (command, DbType.DateTime, ":Expires", Tommorow ()));
194 param.Add (CreateParam (command, DbType.Int32, ":Timeout", timeout));
195 param.Add (CreateParam (command, DbType.Binary, ":StaticObjectsData",
196 session.StaticObjects.ToByteArray ()));
197 param.Add (CreateParam (command, DbType.Binary, ":SessionData",
198 session.SessionDictionary.ToByteArray ()));
200 command.ExecuteNonQuery ();
203 private void UpdateSession (string id, SessionDictionary dict)
205 IDbCommand command = cnc.CreateCommand ();
206 IDataParameterCollection param;
208 string update = "UPDATE ASPStateTempSessions SET " +
209 "SessionData = :SessionData WHERE SessionId = :SessionID";
211 command.CommandText = update;
213 param = command.Parameters;
214 param.Add (CreateParam (command, DbType.String, ":SessionID", id));
215 param.Add (CreateParam (command, DbType.Binary, ":SessionData",
216 dict.ToByteArray ()));
218 command.ExecuteNonQuery ();
221 private IDataParameter CreateParam (IDbCommand command, DbType type,
222 string name, object value)
224 IDataParameter result = command.CreateParameter ();
225 result.DbType = type;
226 result.ParameterName = name;
227 result.Value = value;
231 private DateTime Tommorow ()
233 return DateTime.Now.AddDays (1);
236 private byte [] ReadBytes (IDataReader reader, int index)
238 int len = (int) reader.GetBytes (reader.FieldCount-1, 0, null, 0, 0);
239 byte [] data = new byte [len];
240 reader.GetBytes (index, 0, data, 0, len);