Article ID: 2527105
This article describes an All-In-One framework sample that is available for download. This code sample demonstrates a step-by-step guide that illustrates how to share Session State across Sub Domains.
You can download the sample package from the following icons.
Collapse this imageExpand this image
Download informationTo download this code sample, click one of the following links:
Technical overviewSometimes, developers want to host two or more ASP.NET websites that uses the same session state in multiple subdomains. Therefore, it is easier to make these websites to behavior as the same application and transfer the data across the websites. However, the following problems occur when the websites try to share the same session state:
Sample OverviewThis sample contains an HttpModule (also known as SharedSessionModule) assembly and two ASP.NET web application projects. The HttpModule assembly is used to behave the logic of sharing the session state. Two ASP.NET web application projects are used to demonstrate sharing session state. To share the session state across subdomains, follow these steps:
Configure SQL Server to support the session state. Before you configure SQL Server to support the session state, you have to install SQL Server Express in your operating system.
Note SQL Server Express is normally shipped with Microsoft Visual Studio. To download SQL Server Express, visit the following Microsoft website:
Download SQL Server ExpressThen, run the following command in Console Window:
<system drive>:\Windows\Microsoft.NET\Framework\<.NETFrameworkversion>\aspnet_regsql.exe -S localhost\sqlexpress -E -ssaddNotes
Configure two ASP.NET web applications to use "SQLServer" as the session state mode.To do these, add these settings to the web.config configuration file for the web applications:
Additionally, add these settings to make the web applications to decrypt and validate Session State data by using the same keys:
Note If you host the web applications in Internet Information Services (IIS), run the Application Pool under an account that is able to login the database. Otherwise, you receive the following error message that is generated by the System.Data.SqlClient.SqlException:
Cannot open database 'ASPState' requested by the login. The login failed.
Implement the logic in the HttpModule assembly. To do this, create a new class library that is named "CSASPNETShareSessionBetweenSubDomainsModule" and add a new class that is named "SharedSessionModule" to the project. At the beginning of the file, run the following code to import the necessary namespaces:
Add two static variables to the class. The static variables are used to read settings from the web.config file. The application name represents a particular application when the application is running. The root domain is the domain that is shared by the ASP.NET applications.
Implement the Init() method, write code within the method to change the application name, and then add an event handler to handle the PostRequestHandlerExecute event of current application at the end of the method.
In the PostRequestHandlerExecute event handler, all code is used to modify the ASP.NET_SessionId cookie which is used to store a session ID in the browser.
Configure two ASP.NET web applications to use the HttpModule assembly. To do this, add these settings to the web application projects to enable this HttpModule assembly:
Note If you run the applications with your own domains instead of localhost, you have to change the value of RootDomain after publishing.
LanguagesThis code sample contains the following programming languages:
Collapse this tableExpand this table
What is All-In-One Code Framework?All-In-One Code Framework shows most Microsoft development techniques by using code samples in different programming languages. Each example is carefully selected, composed, and documented to show one common code scenario. For more information about All-In-One Code Framework, visit the following Microsoft website:
How to find more All-In-One Code Framework samplesTo find more All-In-One Code Framework samples, search for "kbcodefx" together with related keywords on the Microsoft support Web site. Or, visit the following Microsoft website:
All-In-One Code Framework samples
For more information about the ASP.NET session state, visit the following MSDN website:
General information about the ASP.NET session stateFor more information about ASP.NET SQL Server registration tool (Aspnet_regsql.exe), visit the following MSDN website:
General information about ASP.NET SQL Server registration toolFor more information about ASP.NET cookies, visit the following MSDN website:
General information about ASP.NET cookiesFor more information about how to Create an ASP.NET HTTP module by using Visual C# .NET, visit the following MSDN website:
How to Create an ASP.NET HTTP module by using Visual C# .NET
Rapid publishing disclaimerMicrosoft corporation and/or its respective suppliers make no representations about the suitability, reliability, or accuracy of the information and related graphics contained herein. All such information and related graphics are provided "as is" without warranty of any kind. Microsoft and/or its respective suppliers hereby disclaim all warranties and conditions with regard to this information and related graphics, including all implied warranties and conditions of merchantability, fitness for a particular purpose, workmanlike effort, title and non-infringement. You specifically agree that in no event shall Microsoft and/or its suppliers be liable for any direct, indirect, punitive, incidental, special, consequential damages or any damages whatsoever including, without limitation, damages for loss of use, data or profits, arising out of or in any way connected with the use of or inability to use the information and related graphics contained herein, whether based on contract, tort, negligence, strict liability or otherwise, even if Microsoft or any of its suppliers has been advised of the possibility of damages.
(http://go.microsoft.com/fwlink/?LinkId=151500)for other considerations.