I did the ASP.net MVC application to deploy as a child application to several sites, you need to get the session value of the site in this application.
The session state server has been used and the machine key has been set, but it still doesn't work.
The problem has been a whole day, and here's the solution.
Build a HttpModule
<summary>
This module needs to be placed on the front of the entire pipeline.
</summary>
public class Sessionsharemodule:ihttpmodule
{
void Ihttpmodule.init (HttpApplication context)
{
Type Storeprovider = typeof (HttpSessionState). Assembly.GetType ("System.Web.SessionState.OutOfProcSessionStateStore");
FieldInfo Urifield = Storeprovider.getfield ("S_uribase", BindingFlags.Static | BindingFlags.NonPublic);
if (Urifield = null)
{
throw new ArgumentException ("Urifield is not Found");
}
Urifield.setvalue (NULL, "xxx.cn");
Context. endrequest + = new EventHandler (this. endrequest);
}
<summary>
After the request has been completed, the domain of all cookies is rewritten as xxx.cn
</summary>
<param name= "Sender" ></param>
<param name= "args" ></param>
void EndRequest (object sender, EventArgs args)
{
HttpApplication application = sender as HttpApplication;
for (int i = 0; i < application. Response.Cookies.Count; i++)
{
Application. Response.cookies[i]. Domain = "xxx.cn";
}
}
void Ihttpmodule.dispose ()
{
}
}
Module deployment
(1) Copy the DLL file of this module to the bin directory of the site and the child application
(2) Add the configuration at the site's Web.config <system.webServer> <modules> section:
<add name= "Sessionshare" type= "XXX". Sessionshare.sessionsharemodule, XXX. Sessionshare "/>
The Web.config configuration entry for a child application can be added without adding. If not, it inherits the site's configuration items. Plus, it overwrites the site's configuration items. It is not recommended to add.
How to allow multiple virtual directories to share sessions under IIS
Each virtual directory is equivalent to an application, where the session is not shared, but your session storage mode set to Inpro,stateserver or Sqlserve, also can not achieve session sharing.
However, if you use the SQL Server database to store sessions, you can share them in a disguised manner as follows:
First, set the session of the virtual directory to be stored on SQL Server:
Step One: Create the ASPState database
Open cmd
Enter: C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727
Perform
Aspnet_regsql.exe-s. -U sa-p 123456-ssadd-sstype P
-S is the server address. Represents a local
-ssadd add
-sstype p session is stored in the ASPState database if it is t there is tempdb
After the session database is created.
Step Two: Web.config Add a configuration node in the virtual directory.
Add in <system.web> node
<sessionstate mode= "SQL Server" sqlconnectionstring= "server=127.0.0.1; Uid=sa; Pwd=sa "cookieless=" false "timeout="/>
Configure the node.
When it's done, the session information is in the database.
Open the ASPState Library of the database, view the stored procedure "[tempgetappid]" Modify it
Find this statement: SET @appName =lower (@appName)
Modify to set @appName = "Portal virtual directory"
Once the session is set up in the Portal virtual directory, the session can be shared elsewhere.