How to use SessionStateProvider in ASP.NET Web Applications with VMware Tanzu GemFire
search cancel

How to use SessionStateProvider in ASP.NET Web Applications with VMware Tanzu GemFire

book

Article ID: 294478

calendar_today

Updated On:

Products

VMware Tanzu Gemfire

Issue/Introduction

How do you use SessionStateProvider in VMware Tanzu GemFire with ASP.NET Web Applications?

This knowledge article describes the essential steps that need to be followed to use SessionStateProvider. The VMware Tanzu GemFire Session library implements Microsoft’s SessionStateStoreProviderBase API. The session state provider stores session state data in a region on the GemFire server.

Environment

Product Version: 10.2
OS: Windows

Resolution

Assuming you have a local or remote GemFire cluster that you have access to and you know the locators with their port numbers, you can avoid adding the GemFire system properties to the web.config for your session state application by adding them to the cache.xml file. However, you will still need to add a region attribute with a value for your region to web.config (this is by design).

For more details on how to use such files, please refer to the doc below:
Note: These instructions are based on .NET NC 10.2.x.


Step 1:

Add a reference to the GemFire .NET Native Client libraries (both Pivotal.GemFire.dll and Pivotal.GemFire.Session.dll) to your projects. 

Note: You must use the matched pair of Pivotal.GemFire.Session.dll and Pivotal.GemFire.dll libraries from the same distribution of the GemFire Native Client. Do not mix two different versions.


Step 2:

Add an element called <sessionState> under the <system.web> section in your web.config file. Then add a  reference to the Pivotal.GemFire.Session.SessionStateStore provider, as given below. You can specify connection pool-related properties here, such as locators, ports, log-level, etc., however, the region still needs to come from the cache.xml file as specified in the geode.properties file (as stated earlier).  The SSL properties are only needed when you are connecting to an SSL-enabled cluster.
<system.web>
    <compilation debug="true" targetFramework="4.5.2"/>
    <httpRuntime targetFramework="4.5"/>
    <pages>
      <namespaces>
        <add namespace="System.Web.Optimization"/>
      </namespaces>
      <controls>
        <add assembly="Microsoft.AspNet.Web.Optimization.WebForms" namespace="Microsoft.AspNet.Web.Optimization.WebForms" tagPrefix="webopt"/>
      </controls>
    </pages>
    <sessionState mode="Custom" cookieless="false" timeout="1" customProvider="GemFireSessionProvider">
      <providers>
        <add name="GemFireSessionProvider" type="Pivotal.GemFire.Session.SessionStateStore"
         region="sessionStateRegion"
         locator-host="localhost"
         locator-port="10334"
         pool-name="default"
         ssl-enabled="true"
         ssl-keystore="C:/gemfire-dotnetsession/SampleWebApp/Security/client_keystore.password.pem"
         ssl-keystore-password="gemstone"
         ssl-truststore="C:/gemfire-dotnetsession/SampleWebApp/Security/client_truststore.pem"
         log-file="C:/gemfire-dotnetsession/SampleWebApp/SampleWebApp.log"
         log-level="fine"
         />
      </providers>
    </sessionState>
  </system.web>


Step 3:

Create a region on the GemFire server(s) that holds the session state for your app:
gfsh> create region --name="sessionStateRegion" --type=PARTITION`
The region name on the server(s) should match the region name specified in the cache.xml file.


Step 4:

Deploy the JAR file that is included on .NET Native Client 10.x distribution, which you downloaded from the Pivotal Network, to the GemFire server(s). You can keep this JAR wherever you wish and can consider it a part of your cluster config by loading it to the server(s)' working directory and then deploying:
gfsh> deploy --jar=YOURPATH/lib/SessionStateStoreFunctions.jar


Step 5:

This is an optional step if your GemFire cluster is configured with authentication using username and password. In this case, you will need to implement an IAuthInitialize interface and then initialize your implemented class under the Application_Start() event in the Global.asax code-behind file:
protected void Application_Start(object sender, EventArgs e)
   {
       SessionStateStore.AuthInitialize = new BasicAuthInitialize("john", "secret");
       RouteConfig.RegisterRoutes(RouteTable.Routes);
       BundleConfig.RegisterBundles(BundleTable.Bundles);
   }

   public class BasicAuthInitialize : IAuthInitialize
   {
       private string _username;
       private string _password;

       public BasicAuthInitialize(string username, string password)
       {
           _username = username;
           _password = password;
       }

       public void Close()
       {
       }

       public Properties<string, object> GetCredentials(Properties<string, string> props, string server)
       {
           var credentials = new Properties<string, object>();

           credentials.Insert("security-username", _username);
           credentials.Insert("security-password", _password);

           return credentials;
       }
   }

For more information on how to configure the geode.properties file, please refer to the knowledge article below:
For a working sample that can be compiled and tested with Visual Studio, you can refer to the following repo: