代码之家  ›  专栏  ›  技术社区  ›  CodeGrue

MVC2:确定重定向到登录是由于窗体身份验证超时造成的?

  •  2
  • CodeGrue  · 技术社区  · 15 年前

    为了显示有意义的消息,当由于窗体身份验证超时而重定向到登录页时,最好的检测方法是什么?

    <authentication mode="Forms">
        <forms loginUrl="~/Login" timeout="15" slidingExpiration="true"/>
    </authentication>
    
    2 回复  |  直到 15 年前
        1
  •  1
  •   CodeGrue    15 年前

    在摆弄了很长一段时间之后,我想出了这个可行的黑客解决方案。我想听听更优雅的解决方案。

    1) 在窗体身份验证后创建cookie:

       // log the user in
       FormsService.SignIn(userId, false);
       Response.Cookies["WasLoggedIn"].Value = "true";
    

    2) 在login GET操作中,查找此cookie existing和authentication cookie not existing:

       if (Request.Cookies[".ASPXAUTH"] == null && 
           Request.Cookies["WasLoggedIn"] != null)
       {
          // forms authentication timed out
       }
    
        2
  •  0
  •   Darin Dimitrov    15 年前

    Authorize 属性,该属性将检查身份验证cookie是否已过期。