Session 配置方式html
web.configgit
<configuration> <system.web> <sessionState mode=" Off | InProc | StateServer | SQLServer " cookieless=" true | false " timeout=" number of minutes " stateConnectionString=" tcpip=server:port " sqlConnectionString=" sql connection string " stateNetworkTimeout=" number of seconds " /> </system.web> </configuration>
mode: Session 的存储方式github
InProc: (默认值) 将Session存到IIS进程内web
Off:禁用Sessionredis
StateServer:将Session存到肚里的状态服务中(ASP .NET State Service)sql
SQLServer:将Session存到SQLServer中数据库
cookieless: 设置客户端的Session存储的方式服务器
false:(默认值)使用Cookie来存储Sessioncookie
true:不使用Cooieless存储Session,而使用url存储session
timeout:设置Session过时时间
(默认值)20
stateConnectionString:设置Session独立存放的状态服务所在的服务器的名称地址和端口
SQLConnectionString:设置与SQLServer的连接字符串
stateNetworkTimeout:设置多少秒后断开web服务器与存储状态信息服务器的TCP/IP连接
(默认值)10
allowCustomSqlDatabase : 容许使用自定义数据库, 使用数据库存储的时候必须设置为 true
Session 三种存储方式
存储在进程中
服务器的Session存储在IIS进程中,当IIS关闭或者重启的时候,这些Session信息就会丢失.
可是能够提高性能
存储在服务中
服务器的Session存储在"ASP.NET State Server"服务进程中.
启动这个服务就能创建一个aspnet_state.exe 的进程
这个进程被用来保存Session
"ASP.NET State Server"服务能够与Web服务器分开
存储在数据库中
服务器的Session存储到SQL Server中
在SQL Server中创建一个存储Session的数据库
aspnet_regsql.exe -S ServerName\InstanceName -U User -P Password -d DBName -ssadd -sstype c
而后在Web.config中配置好信息
在服务和数据库中存储可能发生的错误:
未开启服务:
对存到服务进程中而言, 须要开启 ASP.NETServerState 这项服务
须要序列化会话对象:
这时候须要讲存入数据库中指定的类添加可序列化特性.
而且类中不能有复杂类型的成员变量
利用 redis 来进行存储 Session 并实现 Session 共享
在 Nuget 引用
Install-Package Microsoft.Web.RedisSessionStateProvider
而后配置
<sessionState mode="Custom" customProvider="MySessionStateStore"> <providers> <!-- For more details check https://github.com/Azure/aspnet-redis-providers/wiki --> <!-- Either use 'connectionString' OR 'settingsClassName' and 'settingsMethodName' OR use 'host','port','accessKey','ssl','connectionTimeoutInMilliseconds' and 'operationTimeoutInMilliseconds'. --> <!-- 'throwOnError','retryTimeoutInMilliseconds','databaseId' and 'applicationName' can be used with both options. --> <!-- <add name="MySessionStateStore" host = "127.0.0.1" [String] port = "" [number] accessKey = "" [String] ssl = "false" [true|false] throwOnError = "true" [true|false] retryTimeoutInMilliseconds = "5000" [number] databaseId = "0" [number] applicationName = "" [String] connectionTimeoutInMilliseconds = "5000" [number] operationTimeoutInMilliseconds = "1000" [number] connectionString = "<Valid StackExchange.Redis connection string>" [String] settingsClassName = "<Assembly qualified class name that contains settings method specified below. Which basically return 'connectionString' value>" [String] settingsMethodName = "<Settings method should be defined in settingsClass. It should be public, static, does not take any parameters and should have a return type of 'String', which is basically 'connectionString' value.>" [String] loggingClassName = "<Assembly qualified class name that contains logging method specified below>" [String] loggingMethodName = "<Logging method should be defined in loggingClass. It should be public, static, does not take any parameters and should have a return type of System.IO.TextWriter.>" [String] redisSerializerType = "<Assembly qualified class name that implements Microsoft.Web.Redis.ISerializer>" [String] /> --> <add name="MySessionStateStore" type="Microsoft.Web.Redis.RedisSessionStateProvider" host="Ip地址" port="6379" accessKey="密码" ssl="false" /> </providers> </sessionState>