将会话超时增加到一周或更长时间



为了增加会话超时,我似乎会使用以下设置:

<system.web>
<sessionState mode="InProc" timeout="20" />
/* Etc... */
</system.web>

这里的超时设置为20分钟(默认值)。显然,最大值是525600分钟,也就是一年。

一周后我可以回到Facebook,但我仍然登录。这就是我希望我的应用程序的行为。但根据这个答案,这可能会对性能产生不利影响,因为"您的非活动会话将保留在Web服务器内存中,这可能导致应用程序池回收,从而导致所有用户失去所有会话。">

有人知道这次演出的细节吗?而且,如果这是真的,有没有一种更高效的方法可以让用户像Facebook这样的网站一样登录?

更新:

下面是我当前web.config文件的相关部分。

<system.web>
<authentication mode="None" />
<sessionState mode="InProc" timeout="60" />
<compilation debug="true" targetFramework="4.6" />
<httpRuntime targetFramework="4.5.2" executionTimeout="240" maxRequestLength="20480" />
<httpModules>
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" />
</httpModules>
<customErrors mode="Off"></customErrors>
</system.web>
<system.webServer>
<modules>
<remove name="FormsAuthentication" />
<remove name="ApplicationInsightsWebTracking" />
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" />
</modules>
<validation validateIntegratedModeConfiguration="false" />
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="20971520" />
</requestFiltering>
</security>
</system.webServer>

更新2:

看起来我错误地将两个问题(身份验证和会话状态)混为一谈。我很抱歉没有正确地整理我在谷歌上搜索的一些问题。我的目标只是延长用户登录的时间长度。

对于登录,您必须使用FormsAuthenticationASP.NET Identity(FormsAuthentication之上基于cookie的身份验证的改进版本),这允许您将身份验证cookie保留数周/数月以上。FormsAuthentication是无状态的,为了支持多个服务器,您可以在所有服务器中使用单个machineKey。默认情况下,所有示例和教程主要指导使用FormsAuthentication

Faceboook和所有人都使用身份验证cookie,没有人使用Session登录。

理想情况下,Session是坏的,并且大部分是不必要的。它可以替换为HttpRuntime.Cache。缓存可以很容易地设置为使用一些外部提供程序,如Fabric缓存或Redis。为了使缓存按用户隔离,您可以简单地将缓存项的键附加到用户名中。

更新

使用FormsAuthentication没有任何缺点,只是解密cookie所需的CPU开销很小,但也可以通过缓存身份验证票证来避免。

支持Session的唯一原因可能是与他们可能支持的旧ASP应用程序的兼容性。

在新的ASP。NET MVC示例中,他们在代码中配置了基于cookie的身份验证(在启动时),这不是会话。虽然会话是在web.config中配置的,但只要你不想在会话中存储任何内容,你就可以完全禁用它

从头开始创建了一个股票MVC项目,为Auth选择了个人用户帐户。

启动。Auth.cs

public partial class Startup {
// For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
public void ConfigureAuth(IAppBuilder app) {
// Configure the db context, user manager and signin manager to use a single instance per request
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
// Enable the application to use a cookie to store information for the signed in user
// and to use a cookie to temporarily store information about a user logging in with a third party login provider
// Configure the sign in cookie
app.UseCookieAuthentication(new CookieAuthenticationOptions {
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider {
// Enables the application to validate the security stamp when the user logs in.
// This is a security feature which is used when you change a password or add an external login to your account.  
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
},
ExpireTimeSpan = TimeSpan.FromDays(7)//<-- I just added this.
});
//...code removed for brevity
}
}
// Summary:
//     Controls how much time the cookie will remain valid from the point it is
//     created. The expiration information is in the protected cookie ticket. Because
//     of that an expired cookie will be ignored even if it is passed to the server
//     after the browser should have purged it
public TimeSpan ExpireTimeSpan { get; set; }

没有更改项目中的其他内容,默认模板提供了所需的一切。

更新

根据注释,您可以随时将其添加为web.config中的应用程序设置,并使用ConfigurationManager访问它。这样,就可以在不重新编译代码的情况下对其进行修改。

var expireTimeSpan = TimeSpan.FromDays(7);//the default
var setting = ConfigurationManager.AppSettings["ApplicationCookieExpireTimeInDays"];
if (setting != null) {
var days = 0;
if (int.TryParse(setting, out days)) {
expireTimeSpan = TimeSpan.FromDays(days);
}
}
// Enable the application to use a cookie to store information for the signed in user
// and to use a cookie to temporarily store information about a user logging in with a third party login provider
// Configure the sign in cookie
app.UseCookieAuthentication(new CookieAuthenticationOptions {
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider {
// Enables the application to validate the security stamp when the user logs in.
// This is a security feature which is used when you change a password or add an external login to your account.  
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
},
ExpireTimeSpan = expireTimeSpan
});

其中web.config将保存设置。

<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="ApplicationCookieExpireTimeInDays" value="14" />
</appSettings>

您引用的答案部分正确。这取决于会话状态的存储位置。

在SQL Server数据库中存储会话状态时,增加会话状态应该没有问题。还使用Web Farms——这对于满足可伸缩性是有意义的。

来自本文:

在SQL Server数据库中存储会话状态

在SQL server中存储会话变量有以下内容优点:

可扩展性:如果您正在寻找一个高度可扩展的存储选项您的会话变量,SQL Server选项适用于您。这太多了比其他选项更具可扩展性。Web场架构可以非常轻松访问会话变量,因为它们存储在独立数据库
可靠性:因为数据是物理的持久化在数据库中,它比其他数据库更可靠选项。它能够在服务器重新启动后继续运行
安全性:SQL Server比内存或状态服务器选项更安全。通过配置SQL Server,可以更轻松地保护数据安全

这是一篇老文章,但这些原则仍然适用。

使用Web服务器的内存时可能会出现问题。

增加会话超时对应用程序性能有何影响?为什么

如果延长会话的持续时间,则会话中持有的任何项目变量将在服务器上的内存中停留更长时间。取决于如何你的申请很忙,你的项目类型和数量作为会话变量,这可能会降低性能。

从报价中复制了拼写错误

这个问题还讨论了会话状态和使用FormsAuthentication的cookie之间的区别。

我应该使用会话状态还是FormAuthentication来跟踪已登录用户?

因此,根据您使用的身份验证类型,您可以使用cookie路线,记住用户可以从浏览器中删除cookie,这将使他们注销。

这是指向文档的另一个有用链接。

保护会话状态

相关内容

最新更新