Scaling out web application through multiple instances is one of biggest advantages of azure cloud. To achieve multiple VMs support for our web-role cloud application we are implementing Azure Redis Cache. We are using RedisSessionStateProvider provider for maintaining session state. Following are the configuration settings for session management within web.config file.
<authentication mode="Forms">
<forms loginUrl="~/Login" slidingExpiration="true" timeout="20" defaultUrl="~/Default" />
</authentication>
<sessionState timeout="20" mode="Custom" customProvider="MySessionStateStore">
<providers>
<add name="MySessionStateStore" type="Microsoft.Web.Redis.RedisSessionStateProvider"
host = "dummy.redis.cache.windows.net"
port = "6380"
accessKey = "dummysecretkey"
ssl = "true"
throwOnError = "true"
retryTimeoutInMilliseconds = "5000"
databaseId = "0"
applicationName = ""
connectionTimeoutInMilliseconds = "5000"
operationTimeoutInMilliseconds = "1000"
connectionString = ""/>
</providers>
Our problem is that session timeout is not extending with the user's postback, suppose our user logs into the application at 10:00 AM then his session data will expire at absolute 10:20 AM. If user postbacks at 10:15 AM then session should expire at 10:35 AM but this is not happening it is expiring on 10:20 AM absolute.
Following is the code at login button's click event
protected void Button1_Click(object sender, EventArgs e)
{
FormsAuthentication.SetAuthCookie(TextBox1.Text.Trim(), true);
ConnectionMultiplexer connection = ConnectionMultiplexer.Connec("dummy.redis.cache.windows.net,ssl=true,password=dummysecretkey");
IDatabase cache = connection.GetDatabase();
Session["UserName"] = TextBox1.Text;
Response.Redirect("Default.aspx");
}
I would appreciate if could let me know what needs to be done to get session timeout in sliding mode. Best Regards,
H.R Yadav
Redis client uses a single TCP connection and can only read one response at a time. Even when a first operation times out, it does not stop the data being sent to/from the server. Because of this, it blocks other requests and causes timeouts.
Azure Cache for Redis service configures a 10 minute idle server timeout for connections, independently of the client library used. This is defined on Server side and cannot be changed.
Azure Cache for Redis offers Redis persistence using the Redis database (RDB) and Append only File (AOF): RDB persistence - When you use RDB persistence, Azure Cache for Redis persists a snapshot of your cache in a binary format. The snapshot is saved in an Azure Storage account.
This delay is typically less than one minute. Connections from Azure Cache for Redis monitoring systems are always permitted, even if firewall rules are configured.
Thanks you for reporting the issue. We have released a new version of RedisSessionStateProvider NuGet package that fixes above reported bug.
https://www.nuget.org/packages/Microsoft.Web.RedisSessionStateProvider/1.5.0
EDIT: We have found one more issue with this. ASP.NET doesn't call ResetItemTimeout for AJAX requests and it becomes responsibility of other session state method to slide the session timeout. We have fixed this bug and released a new NuGet package: https://www.nuget.org/packages/Microsoft.Web.RedisSessionStateProvider/1.6.5
Let us know is this resolves your issue or not?
I solved the problem of sliding expiration including the following lines in global.ascx.cs
protected void Application_AcquireRequestState()
{
if (HttpContext.Current.Session != null)
{
RedisSessionStateProvider redis = new RedisSessionStateProvider();
redis.ResetItemTimeout(HttpContext.Current, HttpContext.Current.Session.SessionID);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With