ElastiCache as an ASP.NET Session Store
Are you hosting an ASP.NET application on AWS? Do you want the benefits of Elastic Load Balancing (ELB) and Auto Scaling, but feel limited by a dependency on ASP.NET session state? Rather than rely on sticky sessions, you can use an out-of-process session state provider to share session state between multiple web servers. In this post, I will show you how to configure ElastiCache and the RedisSessionStateProvider from Microsoft to eliminate the dependency on sticky sessions.
Background
An ASP.NET session state provider maintains a user’s session between requests to an ASP.NET application. For example, you might store the contents of a shopping cart in session state. The default provider stores the user’s session in memory on the web server that received the request.
Using the default provider, your ELB must send every request from a specific user to the same web server. This is known as sticky sessions and greatly limits your elasticity. First, the ELB cannot distribute traffic evenly, often sending a disproportionate amount of traffic to one server. Second, Auto Scaling cannot terminate web servers without losing some user’s session state.
By moving the session state to a central location, all the web servers can share a single copy of session state. This allows the ELB to send requests to any web server, better distributing load across all the web servers. In addition, Auto Scaling can terminate individual web servers without losing session state information.
There are numerous providers available that allow multiple web servers to share session state. One option is use the DynamoDB Session State Provider that ships with the AWS SDK for .NET. This post introduces another option, storing session state in an ElastiCache cluster.
ElastiCache is a web service that makes it easy to deploy, operate, and scale an in-memory cache in the cloud. ElastiCache supports both Memcached and Redis cache clusters. While either technology can store ASP.NET session state, Microsoft offers a provider for Redis, and I will focus on Redis here.
Launch an Elasticache for Redis Cluster
Let us begin by launching a new Elasticache for Redis cluster in the default VPC using PowerShell. Note that you can use the ElastiCache console if you prefer.
First, get a reference to the default VPC and create a new security group for the cluster. The security group must allow inbound requests to Redis, which uses TCP port 6379.
$VPC = Get-EC2Vpc -Filter @{name='isDefault'; value='true'}
$Group = New-EC2SecurityGroup -GroupName 'ElastiCacheRedis' -Description 'Allows TCP Port 6379'
Grant-EC2SecurityGroupIngress -GroupId $Group -IpPermission @{ IpProtocol="tcp"; FromPort="6379"; ToPort="6379"; IpRanges=$VPC.CidrBlock }
Second, launch a new Redis cluster. In the example below, I launch a single node cluster named “aspnet” running on a t2.micro. Make sure you specify the security group you created above.
New-ECCacheCluster -CacheClusterId 'aspnet' -Engine 'redis' -CacheNodeType 'cache.t2.micro' -NumCacheNode 1 -SecurityGroupId $Group
Finally, get the endpoint address of the instance you just created. Note that you must wait a few minutes for the cluster to launch before the address is available.
(Get-ECCacheCluster -CacheClusterId 'aspnet' -ShowCacheNodeInfo $true).CacheNodes[0].Endpoint.Address
The endpoint address is a fully qualified domain name that ends in cache.amazon.com and resolves to a private IP address in the VPC. For example, ElastiCache assigned my cluster the address below.
aspnet.k30h8n.0001.use1.cache.amazonaws.com
Configuring the Redis Session State Provider
With the Redis cluster running, you are ready to add the RedisSessionStateProvider to your ASP.NET application. Open your project in Visual Studio. First, right click on the project in Solution Explorer and select Manage NuGet Packages. Then, search for “RedisSessionStateProvider” and click the Install button as show below.
NuGet will add a custom session state provider to your project’s web.config file. Open the web.config file and locate the Microsoft.Web.Redis.RedisSessionStateProvider shown below.
<sessionState mode="Custom" customProvider="MySessionStateStore">
<providers>
<add name="MySessionStateStore" type="Microsoft.Web.Redis.RedisSessionStateProvider" host="127.0.0.1" accessKey="" ssl="false" />
</providers>
</sessionState>
Now replace the host attribute with the endpoint address you received from Get-ECCacheCluster. For example, my configuration looks like this.
<sessionState mode="Custom" customProvider="MySessionStateStore">
<providers>
<add name="MySessionStateStore" type="Microsoft.Web.Redis.RedisSessionStateProvider" host="aspnet.k30h8n.0001.use1.cache.amazonaws.com" accessKey="" ssl="false" />
</providers>
</sessionState>
You are now ready to deploy and test your application. Wasn’t that easy?
Summary
You can use ElastiCache to share ASP.NET session information with multiple web servers and eliminate the dependency on ELB stick sessions. ElastiCache is simple to use and integrates with ASP.NET using the RedisSessionStateProvider available as a NuGet package. For more information about ElastiCache, see the ElastiCache documentation.
相關推薦
ElastiCache as an ASP.NET Session Store
Are you hosting an ASP.NET application on AWS? Do you want the benefits of Elastic Load Balancing (ELB) and Auto Scaling, but feel limited by a d
大型Web 站點 Asp.net Session過期你怎麽辦
站點 ria send rac 請求 問題 btn b2c title 在 WEB 系統中。 我們通常會用session來保存一些簡單可是卻非常重要的信息。比方Asp.net中常常會用Session來保存用戶登錄信息,比方UserID。為了解決 WEB場大家採用了把s
ASP.NET Session
.aspx 狀態 spa 第一個 get 主頁 .net 刷新 打不開 Session 保存在服務器上 缺點;占用服務器資源 優點:安全 不會存持續性的,大量的東西只有會話模式 20分鐘生命周期 當瀏覽器關閉時,Session不在了,關閉了,打不開了網頁 web端
一個隊asp.net session進行了再次封裝的C#類的程式碼
將寫內容過程經常用到的內容片段做個收藏,下邊內容段是關於一個隊asp.net session進行了再次封裝的C#類的內容。 using System.Web; namespace DotNet.Utilities { public static class SessionHelper2
一個隊asp.net session進行了再次封裝的C#類的代碼
out time oid 再次 current web mes get pub 將寫內容過程經常用到的內容片段做個收藏,下邊內容段是關於一個隊asp.net session進行了再次封裝的C#類的內容。 using System.Web; namespace DotNe
asp.net session超時的解決辦法
第一個辦法: 我有好辦法。並且一直用,感覺很爽。1、在web.config裡面加入:<sessionState mode="StateServer" stateConnectionString="tcpip=127.0.0.1:42424" cookieless ="f
ASP.NET Session丟失問題解決方案總結及判斷Session是否過期【轉載】
判斷Session是否過期: 通過BasrPage或IHtttpMoudle實現public class BasePage : System.Web.UI.Page { public BasePage() { } prot
Asp.Net Session生命週期
Asp.Net中的Session與Cookie最大的區別在於:Cookie資訊全部存放於客戶端,Session則只是將一個ID存放在客戶端做為與服務端驗證的標記,而真正的資料都是放在服務端的記憶體之中的。 在傳統web程式語言(比如asp)中,session的... &nb
Asp.net基於session實現購物車的方法
lai 程序 clas contain ext info border mode man 本文實例講述了asp.net基於session實現購物車的方法。分享給大家供大家參考,具體如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1
ASP.Net MVC Session和Cookies的簡單使用
span pen ons -- req ria hide resp request 目標:用Session和Cookies實現登陸信息保存和展現 Cookies實現: Controller: //把登陸用戶名存到cookies中 HttpCookie cook =
Asp.Net程序根目錄下文件夾操作導致Session失效的解決方案
rgs get bject process simple exce cep clas .net 1、配置web.config <system.web> <sessionState mode="StateServer" stateConnectio
asp.net MVC 中 Session統一驗證的方法
pla sta tails project ssi 一個 str urn string 驗證登錄狀態的方法有:1 進程外Session 2 方法過濾器(建一個類繼承ActionFilterAttribute)然後給需要驗證的方法或控制器加特性標簽 3 :新建一個Ba
ASP.NET MVC判斷基於Cookie的Session過期
new pac direct net ttr container att thum tpc 當我們第一次請求訪問時,可以看到Response的Set-Cookie裏添加了ASP.NET_SessionId的值,以後再訪問時可以看到Resquest裏的Cookie已經包含這個
ASP.NET頁面之間傳值的方式之Session(個人整理)
lec 傳遞 button doctype http 參考 方式 創建 logs Session Session在ASP.NET中,表示客戶端(Goggle,Firefox,IE等)與服務器端的會話,用來存儲特定會話信息,準確來說,是用來存儲特定用戶信息。當客戶端向服務
Asp.net core如何使用Session
host con pre ner mvc -s bsp bubuko 使用 Asp.net core使用session: 在新建Asp.net core應用程序後,要使用session中間件,在startup.cs中需執行三個步驟: 1.使用實現了IDistributedc
asp.net使用session完成: 從哪個頁面進入登錄頁面,登錄成功還回到那個頁面
redirect net resp rec isp response AS edi != 1、在Login.aspx頁面Load中加入 if (!IsPostBack && Request.UrlReferrer != null) {
源碼分析Session的臺前幕後(Asp .Net MVC5)
加工 splay text gif mic close bsp nag pre 在這篇文章裏,我們從源代碼的角度重點分析Session的創建、緩存、銷毀、管理。 通常我們說的Session指的是在控制器中使用的Session字段,該字段的類型是HttpSession
asp.net core session的使用
code 命名空間 項目執行 ati art ise .get TP 介紹 Session介紹 本文假設讀者已經了解Session的概念和作用,並且在傳統的.net framework平臺上使用過。 Asp.net core 1.0好像需要單獨安裝,在nuget控制臺,
微軟與開源幹貨對比篇_PHP和 ASP.NET在 Session實現和管理機制上差異
ima 影響 前言 很難 刪除 定性 缺點 重新編譯 state 微軟與開源幹貨對比篇_PHP和 ASP.NET在 Session實現和管理機制上差異 前言:由於開發人員要靠工具吃飯,可能和開發工具、語言、環境呆的時間比和老婆孩子親人在一起的時間還多,所以每個人或多或少對自
ASP.NET Core 2 學習筆記(十一)Cookies & Session
自動 asp Go 內存 rtu serialize .html call names 原文:ASP.NET Core 2 學習筆記(十一)Cookies & Session基本上HTTP是沒有記錄狀態的協定,但可以通過Cookies將Request來源區分出來,並