0

我有一个使用 Forms Auth 的网站。客户端根本不希望用户的站点会话过期。在登录页面代码隐藏中,使用了以下代码:

// user passed validation
FormsAuthentication.Initialize();

// grab the user's roles out of the database 
String strRole = AssignRoles(UserName.Text);

// creates forms auth ticket with expiration date of 100 years from now and make it persistent
FormsAuthenticationTicket fat = new FormsAuthenticationTicket(1,
  UserName.Text, DateTime.Now,
  DateTime.Now.AddYears(100), true, strRole,
  FormsAuthentication.FormsCookiePath);

// create a cookie and throw the ticket in there, set expiration date to 100 years from now
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, 
  FormsAuthentication.Encrypt(fat)) { Expires = DateTime.Now.AddYears(100) };

// add the cookie to the response queue
Response.Cookies.Add(cookie);

Response.Redirect(FormsAuthentication.GetRedirectUrl(UserName.Text, false));

web.config 文件的 auth 部分如下所示:

<authentication mode="Forms">
      <forms name="APLOnlineCompliance" loginUrl="~/Login.aspx" defaultUrl="~/Course/CourseViewer.aspx" />
</authentication>

当我登录该站点时,我确实看到cookie 被正确发送到浏览器并传回:

HttpFox 输出 http://cid-e79f8e4b07c3e30f.office.live.com/embedphoto.aspx/Public/SessionProblem.png

然而,当我离开 20 分钟左右,回来尝试在网站上做任何事情时,登录窗口再次出现。这个解决方案在我们的服务器上运行了一段时间 - 现在它又回来了。在 VS2008 中运行 Cassini 的本地开发盒上不会出现此问题。

有想法该怎么解决这个吗?

4

5 回答 5

2

会话超时和表单身份验证超时是两个不同的东西。会话超时是否设置为 20 分钟,它是否会在 Global.asax 文件中的 Session_End 事件中将您的用户注销?

于 2010-06-11T20:23:50.553 回答
1

默认情况下,IIS 6 中的应用程序池设置为在 20 分钟不活动后关闭。如果您的应用配置中没有任何内容导致您的应用快速关闭,请检查 IIS 管理器中的应用池配置。你可以在那里设置很多很棒的旋钮。

于 2010-08-19T14:45:20.593 回答
0

您可能想要检查您是否正在使用负载均衡器。如果是这样,那么你真的不应该存储 InProc。如果您有多个实体,则应查看状态服务器或 sql 服务器。

基于这个问题,似乎也没有遵守默认的 30 分钟,这通常指向 IIS/Hosting/Network 配置。

于 2011-11-13T15:21:45.767 回答
0

好吧,我在 Global.asax 中有以下内容:

protected void Application_AuthenticateRequest(Object sender, EventArgs e)
    {
        //Fires upon attempting to authenticate the use
        if (!(HttpContext.Current.User == null))
        {
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                if (HttpContext.Current.User.Identity.GetType() == typeof(FormsIdentity))
                {
                    FormsIdentity fi = (FormsIdentity) HttpContext.Current.User.Identity;
                    FormsAuthenticationTicket fat = fi.Ticket;

                    String[] astrRoles = fat.UserData.Split('|');
                    HttpContext.Current.User = new GenericPrincipal(fi, astrRoles);
                }
            }
        }
    }

你指的是这个吗?此外,如果这有什么不同的话,我们处于 IIS6 环境中。

于 2010-06-11T20:29:16.470 回答
0

另一个快速检查的事情可能是您的托管类型。云主机通常会有一个负载均衡器,很难设置相同的 IP 指向节点服务器约 20 分钟,但在此之后,您可能会被推送到新服务器,在新服务器上创建新会话并“记录您”出去'

如果您使用标准共享主机或单个专用服务器或虚拟服务器,那么这不会是问题:)

要解决此问题并保持 asp.net 会话正常工作,您需要将会话状态移动到数据库 - 或重新工具您的代码以完全不使用会话:)

于 2011-07-25T05:25:56.007 回答