代码之家  ›  专栏  ›  技术社区  ›  Andre Pena

为什么.aspaux cookie没有通过FormsAuthentication验证?

  •  1
  • Andre Pena  · 技术社区  · 14 年前

    我有一个使用FormsAuthentication的网站,是的,cookie的名称是.aspaux:。

    我可以完全登录。服务器创建表单身份验证票证,将其打包到cookie中,正确确定到期时间(提前1年)并将其发送到客户端。

    出于某种原因,一段时间后,即使cookie还在(我可以用firecokes看到),httpcontext.current.request.isauthenticated在服务器上也会变为false。好像无法验证cookie。

    问题是:为什么会这样?如何调试为什么cookie突然变得无效而不过期?

    编辑

    登录方法如下:

    public static bool Login(int id)
            {
                try
                {
                    string securityToken = UserHelper.AuthenticateUser(id);
    
                    DateTime expiryDate = DateTime.Now.AddYears(1);
                    FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
                         1, id.ToString(), DateTime.Now, expiryDate, true,
                         securityToken, FormsAuthentication.FormsCookiePath);
    
                    string encryptedTicket = FormsAuthentication.Encrypt(ticket);
                    HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
                    cookie.Expires = expiryDate;
    
                    HttpContext.Current.Response.Cookies.Add(cookie);
    
                    return true;
                }
                catch
                {
                    return false;
                }
            }
    

    和web.config:

    <authentication mode="Forms">
                <forms loginUrl="~/Login.aspx" timeout="2880" slidingExpiration="true"/>
            </authentication>
    
    2 回复  |  直到 14 年前
        1
  •  2
  •   Neil Fenwick    14 年前

    在web.config中设置静态机器密钥,以确保生成票据时使用的加密密钥能够在回收的应用程序池(或在ASP.NET Web服务器中重新启动您的网站)中生存下来?

    另请参阅本文档的“窗体身份验证票证”部分。 MSDN library article

        2
  •  0
  •   Richard Szalay    14 年前

    我能想到的几个问题:

    您有多个域(包括 www.domain.com VS domain.com )?

    如果是,则将cookie中的域设置为 域名网 或者确保始终使用相同的域

    你在使用HTTPS吗?

    如果是这样,请确保始终通过https访问cookie或确保 Secure 在上设置为false HttpCookie (否则只能在https请求上访问)

    您正在从虚拟目录中写入cookie吗?

    如果是这样,则可能会设置cookie上的“路径”,并且无法从路径外部访问该路径。

    您有多个Web服务器吗?

    如果是这样,请确保您的机器密钥设置为相同的值(尽管这会引发异常)

    推荐文章