Skip to content

vebin/MongoDB-ASP.NET-Session-State-Store

 
 

Repository files navigation

Note for this fork

1 - Based on official MongoDb.Driver 2.0, and working fine on MVC5.

2 - Tests in this solution are all passed. Check the test projects may help you pinpointing weird stuff quickly.

Usage

1 - Install the nuGet package into your solution. (The original verion, not this fork)

2 - Into web.config file add a section as detailed following set connection parameters properly.

    <configuration>
      <connectionStrings>
        <add name="MongoSessionServices"
             connectionString="mongodb://mongo1:27018,mongo1:27019,mongo1:27020/?connect=replicaset"/>
      </connectionStrings>
    </configuration>

3 - Configure the provider section as detailed following:

    <system.web>
    <sessionState mode="Custom" customProvider="MongoSessionStateProvider">
      <providers>
        <add name="MongoSessionStateProvider"
             type="MongoSessionStateStore.MongoSessionStateStore"
             connectionStringName="MongoSessionServices" />
      </providers>
    </sessionState>
    </system.web>

Now you can get started using MongoDB Session State Store.

A helper file is provided with the nuget package and this file will be available in the target project when package has been installed. It's strongly recommended to use these helpers as shown in the examples, also you can extend it.

// Sets 1314 in key named sessionKey
Session.Mongo<int>("sessionKey", 1314);

// Gets the value from key named "sessionKey"
int n = Session.Mongo<int>("sessionKey");

/*Seemed when retrive int value from mongosession, it should be converted to long. like:
	long ln=Session.Mongo<long>("sessionKey");
	int n=(int)ln; //if for sure.
*/

/* Note that decimal values must be converted to double.
   For non primitive objects you can use the same helper methods. */

// Creates and store the object personSetted (Person type) in session key named person
Person personSetted = new Person()
	{
		Name = "Marc",
		Surname = "Cortada",
		City = "Barcelona"
	};
Session.Mongo<Person>("person", personSetted);

// Retrieves the object stored in session key named "person"
Person personGetted = Session.Mongo<Person>("person");

Also, for primitive types you can use a direct way (not recommended).

// Set primitive value
Session["counter"] = 1;

//Get value from another request
int n = Session["counter"];

For further information read about parameters config

This is a major release, so keep in mind these compatibility notes.

To make session data without expiration time disable TTL index creation (do not use Session.Timeout value).

Packages

No packages published

Languages

  • C# 85.9%
  • Classic ASP 7.6%
  • HTML 6.5%