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>