在FormsAuthentication对用户进行身份验证后,撤消身份验证并重定向到登录页面

时间:2008-12-01 22:35:05

标签: c# asp.net forms-authentication

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

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

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

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

    1. 再次重定向到相同的网址 - 这应该有效,因为这次表单身份验证将失败,并且它将重定向到登录页面。

    2. 抛出一些异常???哪一个使重定向发生而我没有指定任何东西?

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

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

    5. 还有其他想法吗?

2 个答案:

答案 0 :(得分:3)

我认为没有一种自动化的方式来实现这一目标。 我认为最好的方法是在auth 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分钟内已删除的用户,并检查该集合。

希望有所帮助。

答案 1 :(得分:0)

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