DocumentDB for sessions

Created: 8 Mar 2017, last update: 24 Jun 2017

Using Azure DocumentDB as Sitecore session Database

With the latest Sitecore 8.2 you can use out of the box Redis Cache for the session database. This is an excellent choose for a session database in Azure. For Sitecore 8.1 you can choose between SQL or MongoDB. (Or use the latest Sitecore Azure package to add Redis session to Sitecore 8 or Sitecore 8.1. But, question: Is it possible to use an Azure DocumentDB as session database?.

With a small adjustment it is possible to use DocumentDB (with the MongoDB API) as xDB database but this fix is not for the session provider.
You can read it here, it is not supported or recommended to use in production but it is possible:

using-azure-documentdb-with-sitecore-xdb
Using-Azure-document-Db-as-Mongo-provider

If you try to use the MongoDB session provider with an Azure DocumentDB you have the same SSL/TLS issue as describe in the linked blogs. I did not found a similar pipeline for the session provider. I take JetBrains dotPeek and modified the connection code to match the Azure DocumentDB strict security requirements and standards. DocumentDB accounts require authentication and secure communication via SSL a secure TLS 1.2 connection, Transport Layer Security (TLS).

In the MongoSessionStateStore.cs I change the GetCollection method to this:

    private static MongoCollection GetCollection(string connectionString, string collectionName)
    {
        MongoUrl url = new MongoUrl(connectionString);

        MongoClientSettings settings = MongoClientSettings.FromUrl(url);
        settings.UseSsl = true;
        settings.VerifySslCertificate = false;
        settings.SslSettings = new SslSettings();
        settings.SslSettings.EnabledSslProtocols = SslProtocols.Tls12;

        return (MongoCollection) new MongoClient(settings).GetServer().GetDatabase(url.DatabaseName).GetCollection(collectionName);
    }

    private static MongoCollection GetCollection(string connectionString, string databaseName, string collectionName)
    {
            MongoUrl url = new MongoUrl(connectionString);

            MongoClientSettings settings = MongoClientSettings.FromUrl(url);
            settings.UseSsl = true;
            settings.VerifySslCertificate = false;
            settings.SslSettings = new SslSettings();
            settings.SslSettings.EnabledSslProtocols = SslProtocols.Tls12;

            return (MongoCollection) new MongoClient(settings).GetServer().GetDatabase(databaseName).GetCollection(collectionName);
    }

And refactor the namespace to Sitecore.SessionProvider.AzureMongoDB. Changed the sessionState provider in the web.config to

<sessionState mode="Custom" customProvider="mongo" cookieless="false" timeout="20">
    <providers>
        <add name="mongo"
            type="Sitecore.SessionProvider.AzureMongoDB.MongoSessionStateProvider, 
            Sitecore.SessionProvider.AzureMongoDB"
            connectionStringName="session"
            pollingInterval="2"
            compression="true"
            sessionType="private"/>
    </providers>   
</sessionState>

And it works. It adds the sessions to the DocumentDB database and delete the records after expire. If you want the dll for Sitecore 8.1 update-3 download it here: Sitecore.SessionProvider.AzureMongoDB.DLL