The <system.web> node for all sites that want to share Web. config is added first.
<!--session of Mode=stateserver-->
<sessionstatecookieless= "false"Timeout= " the"Mode= "StateServer"stateconnectionstring= "tcpip=127.0.0.1:42424"/>
<!--machine code to be unified--<MachineKeydecryptionkey= "fd69b2eb9a11e3063518f1932e314e4aa1577bf0b824f369"validationkey= "5f32295c31223a362286dd5777916fcd0fd2a8ef882783fd3e29ab1fcdfe931f8fa45a8e468b7a40269e50a748778cbb8db2262d44a86bbcea9 6DCA46CBC05C3 "Validation= "SHA1"decryption= "Auto"/> <httpmodules>
<add name= "Crossdomaincookiemodule" type= "Weblibrary.crossdomaincookie, weblibrary"/>
</ httpmodules >
Create a new class library project code as follows
usingsystem.web;usingSystem;usingSystem.Configuration;usingSystem.Diagnostics;usingSystem.Reflection;usingSystem.Web.SessionState; namespaceWeblibrary { Public classCrossdomaincookie:ihttpmodule {Private stringM_rootdomain ="http://localhost:8008/";//main shared session URL, here Tester so with Locahlost, can also use domain name#regionIHttpModule Members Public voidDispose () {} Public voidInit (HttpApplication context) {M_rootdomain= configurationmanager.appsettings["Rootdomain"]; Type Stateserversessionprovider=typeof(HttpSessionState). Assembly.GetType ("System.Web.SessionState.OutOfProcSessionStateStore"); FieldInfo Urifield= Stateserversessionprovider.getfield ("S_uribase", BindingFlags.Static |bindingflags.nonpublic); if(Urifield = =NULL) Throw NewArgumentException ("Urifield is not found"); Urifield.setvalue (NULL, M_rootdomain); Context. EndRequest+=NewSystem.EventHandler (context_endrequest); } voidContext_endrequest (Objectsender, System.EventArgs e) {HttpApplication app= Sender asHttpApplication; for(inti =0; I < App. Context.Response.Cookies.Count; i++) {app. Context.response.cookies[i]. Domain=M_rootdomain; } } #endregion } }
If StateServer is not 127.0.0.1 for the remote server, modify the entries in the session server registry for the remote server IP address IPAddress hkey_local_machine\system\ The value of the Allowremoteconnection key in CurrentControlSet\Services \aspnet_state\parameters is 1, where the port key controls the ASP. The service's listening port, which requires the restart of the ASP. NET State service to take effect after modification;
. NET multi-site implementation session sharing via StateServer