# ASP.NET session state store provider for MySql

> Custom ASP.NET session state provider for MySQL databases. Alternative to SQL Server for storing web application session data.

*Published July 10, 2007* · Categories: net

Source: https://www.kimpel.com/2007/07/10/asp-net-session-state-store-provider-for-mysql/


As I mentioned earlier here, my website ultimately is .NET ready ![Smiley face](https://cdn.kimpel.com/smile_regular.gif).

Since I have quite a number of photos online that are off the record, I wanted to 'protect' them using forms authentication (within the former web hosting package developed in PHP I was using .htaccess and .htpassword files in order to protect complete directories).

So, from my professional experience, I know that saving data in session is feasible. However, the problem comes with a clustered environment that you usually run into in production environments. One possible solution now is to store session data in a database. Since the package I ordered at Easy CGI does not come with a MS SQL Server database (unless you add it as an additional feature to your package) you are limited to MS Access or MySql as your database management system ... well, this actually isn't that much of a choice since MS Access is not really suitable in a multi-user environment.

Looking on the web I was not able to find a suitable solution that uses MySql as its session state store. It may certainly be possible that there is already a better solution available (if so, please do not hesitate to contact me). However, I was able to find a sample session state store provider using MS Access. That's it! Port it to MySql ... and that's what I did.

But, let's do it step by step. The list below gives you an idea on what I did in order to get it to work:

1. Downloaded the sample session state store provider for MS Access from **MSDN**, original URL <https://learn.microsoft.com/en-us/previous-versions/aspnet/ms178589(v=vs.100)>.
2. Downloaded the .NET Connector (using version 5.0.7) from [MySql](https://dev.mysql.com/downloads/connector/net/5.0.html).
3. Ported the sample code provided by Microsoft for use with MySql (the zipped source code can be found <https://www.kimpel.com/FileDownloader.aspx?file=MySqlSessionStateStore> v.0.1.zip).
4. Adjusted the Web.config accordingly:

```xml
<configuration xmlns\="https://schemas.microsoft.com/.NetConfiguration/v2.0"\>
  <connectionStrings\>
    <add name\="MySqlSessionServices" connectionString\="Database=<name of database>; Data Source=<host>; User Id=<login>; Password=<password>"/>
  </connectionStrings\>
  <system.web\>
  <sessionState cookieless\="false" regenerateExpiredSessionId\="true" mode\="Custom" customProvider\="MySqlSessionProvider"\>
  <providers\>
    <add name\="MySqlSessionProvider" type\="Samples.AspNet.Session.MySqlSessionStateStore" connectionStringName\="MySqlSessionServices" writeExceptionsToEventLog\="false"/>  
  </providers\>  
  </sessionState\>
  </system.web\>
</configuration\>
````

You can check it out at [www.kimpel.com](https://www.kimpel.com/) ... it works like a charm ![Winking smiley](https://cdn.kimpel.com/smile_wink.gif).

Please do not hesitate to contact me if you have any questions and/or comments.

