1

如果用户不再存在(或某些其他情况),我需要在表单身份验证机制已经从浏览器接收到身份验证 cookie 并对其进行验证之后撤销身份验证 cookie。即这里是使用场景:

  1. 用户已通过身份验证,并被授予未过期的身份验证 cookie。
  2. 几天后,用户再次尝试访问我的网络应用程序,并且由于 cookie 有效,表单身份验证机制将授予访问权限。

  3. 现在我想执行第二次检查(无论我想要什么条件),并决定是让用户继续,还是撤销身份验证。

问题是 - 是否有官方的自动化方式?到目前为止,我提出了一些可能性,但我不知道哪个更好。我可以在 global.asax 中捕获 Authenticate 事件,检查我想要的任何内容,并撤销我清除 cookie,然后其中之一:

  1. 再次重定向到相同的 url - 这应该可以工作,因为这次表单身份验证将失败,它将重定向到登录页面。

  2. 抛出一些异常???在没有我指定任何内容的情况下,哪一个可以使重定向发生?

  3. 以某种方式从配置文件中获取登录页面 url(任何想法如何/使用哪个配置处理程序)并直接重定向?

  4. 我忽略了一些 FormsAuthentication 类/方法,它是为此设计的?

  5. 还有什么想法吗?

4

2 回答 2

3

我认为没有一种自动化的方法可以实现这一目标。我认为最好的方法是向身份验证 cookie 添加一个日期,这将是您最后一次检查用户是否存在。因此,当用户登录时,您将:

FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
                1, // Ticket version
                name, // Username associated with ticket
                DateTime.Now, // Date/time issued
                DateTime.Now.AddMonths(1), // Date/time to expire
                true, // "true" for a persistent user cookie
                DateTime.Now.ToUniversalTime(), // last time the users was checked
                FormsAuthentication.FormsCookiePath);// Path cookie valid for

        // Encrypt the cookie using the machine key for secure transport
        string hash = FormsAuthentication.Encrypt(ticket);
        HttpCookie cookie = new HttpCookie(
            FormsAuthentication.FormsCookieName, // Name of auth cookie
            hash); // Hashed ticket

        cookie.HttpOnly = true;

        // Set the cookie's expiration time to the tickets expiration time
        if (ticket.IsPersistent) cookie.Expires = ticket.Expiration;
        //cookie.Secure = FormsAuthentication.RequireSSL;
        Response.Cookies.Add(cookie);

然后,每次对用户进行身份验证时,您都可以检查传递给身份验证票证的附加日期,并以 10 分钟或更短的时间间隔再次检查数据库是否存在用户。代码可能如下所示:

public void FormsAuthentication_OnAuthenticate(object sender, 
                           FormsAuthenticationEventArgs args)
    {
        if (FormsAuthentication.CookiesSupported)
        {
            if (Request.Cookies[FormsAuthentication.FormsCookieName] != null)
            {
                try
                {
                    FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(
                      Request.Cookies[FormsAuthentication.FormsCookieName].Value);

                    DateTime lastCheckedTime = DateTime.TryParse(ticket.UserData);
                    TimeSpan elapsed = DateTime.Now - lastCheckedTime;
                    if (elapsed.TotalMinutes > 10)//Get 10 from the config
                    {
                        //Check if user exists in the database. 
                        if (CheckIfUserIsValid())
                        {
                            //Reset the last checked time
                            // and set the authentication cookie again
                        }
                        else
                        {
                            FormsAuthentication.SignOut();
                            FormsAuthentication.RedirectToLoginPage();
                            return;
                        }
                    }

                }
                catch (Exception e)
                {
                    // Decrypt method failed.
                }
            }
        }
    }

您甚至可以缓存在过去 10 分钟内被删除的用户并检查该集合。

希望有帮助。

于 2008-12-01T23:59:55.420 回答
0

如果您因为会话过期以外的其他原因拒绝 cookie,那么我认为您应该将用户重定向到一个页面,该页面描述了他们需要做什么才能获得访问权限。如果再次登录就足够了,那么登录页面就足够了。但是,这听起来像是在某些情况下无法简单地再次登录。在这些情况下,最好将用户重定向到一个合适的错误页面,该页面描述了他们无法访问该站点的原因并解释了如何再次获得访问权限(如果可能)。

于 2008-12-02T02:33:51.460 回答