问题
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
回答1:
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?
回答2:
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);
}
}
回答3:
You have to reset the key yourself (after you load it):
bool KeyExpire(RedisKey key, DateTime? expiry, CommandFlags flags = CommandFlags.None);
bool KeyExpire(RedisKey key, TimeSpan? expiry, CommandFlags flags = CommandFlags.None);
来源:https://stackoverflow.com/questions/28292265/session-timeout-is-not-sliding-in-azure-redis-cache-session-state-provider