进程外session

进程外session

A  SqlServer

1.管理员身份运行cmd

2.更换目录  cd c:WindowsMicrosoft.NETFrameworkv4.0.30319>

3.aspnet_regsql.exe -ssadd -sstype p -S 127.0.0.1 -U sa -P 12345

4.web.config -system.web下添加节点

<sessionState mode="SQLServer" timeout="100" sqlConnectionString="server=.;uid=sa;password=12345;">

</sessionState>

B StateServer

使用aspnet_state.exe

1.开启服务:我的电脑-管理-服务与应用程序-服务-ASP.NET State Service(ASP.NET 状态服务)

2.修改配置文件:
<system.web>节点中添加<sessionState mode="StateServer" stateConnectionString="tcpip=localhost:42424"/>ASP.NET 状态服务端口号默认:42424

3.ASP.NET 状态服务只限本机使用如果需要保存外部的Session需要修改注册表

设置是否允许远程使用,位置:C:WINDOWSMicrosoft.NETFrameworkv4.0.30319
HKEY_LOCAL_MACHINESYSTEMCurrentControlSetServicesaspnet_stateParametersAllowRenoteConnection值改为 1

测试代码如下:

using System;
namespace WebForm {
    public partial class OutSession : System.Web.UI. Page {
        protected void Page_Load( object sender, EventArgs e) {
            if (Session["key" ] != null) {
                Person p = (Person )Session[ "key"];
                Response.Write(p.GetName());
            } else {
                Session[ "key" ] = new Person( "piziyimao" );
            }
        }
    }
    [ Serializable] //必须是可序列化的类
    public class Person {
        public Person(string n) {
            name = n;
        }
        private string name;
        public string GetName() {
            return name;
        }
    }
}

Web.Config

<? xml version =" 1.0 "?>
< configuration>
       < system.web >
              < compilation debug = "true " targetFramework = "4.0 " />
        < sessionState mode = "StateServer "
                      stateConnectionString =" tcpip=localhost:42424 "/>
       </ system.web >
       < connectionStrings >
              < add name = "ConnStr " connectionString =" server=.;database=School;uid=sa;pwd=123456 " />
       </ connectionStrings >
</ configuration>

参考文件http://blog.csdn.net/dyllove98/article/details/8698397

原文地址:https://www.cnblogs.com/John-Marnoon/p/5816691.html