1

我正在处理 mvc.net 中的自定义登录页面。我像这样检查登录名:

public bool Login(string login, string password, bool persistent)
{
  var loginEntity = this.AdminRepository.GetLogin(login, password);
  if (loginEntity != null)
  {
    FormsAuthentication.SetAuthCookie(login, persistent);

    HttpContext.Current.Session["AdminId"] = loginEntity.AdminId;
    HttpContext.Current.Session["AdminUsername"] = loginEntity.Username;

  return true;
  }

然后我用过滤器属性装饰任何需要管理员访问权限的控制器:

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
  var ctx = HttpContext.Current;

  // check if session is supported
  if (ctx.Session != null)
  {
    var redirectTargetDictionary = new RouteValueDictionary();

    // check if a new session id was generated
    if (ctx.Session.IsNewSession)
    {
        // If it says it is a new session, but an existing cookie exists, then it must
        // have timed out
        string sessionCookie = ctx.Request.Headers["Cookie"];
        if (((null != sessionCookie) && (sessionCookie.IndexOf("ASP.NET_SessionId") >= 0)) || null == sessionCookie)
        {
          redirectTargetDictionary = new RouteValueDictionary();
          redirectTargetDictionary.Add("area", "Admin");
          redirectTargetDictionary.Add("action", "LogOn");
          redirectTargetDictionary.Add("controller", "Home");

          filterContext.Result = new RedirectToRouteResult(redirectTargetDictionary);
        }
      } else if (SessionContext.AdminId == null) {
        redirectTargetDictionary = new RouteValueDictionary();
        redirectTargetDictionary.Add("area", "Admin");
        redirectTargetDictionary.Add("action", "LogOn");
        redirectTargetDictionary.Add("controller", "Home");

        filterContext.Result = new RedirectToRouteResult(redirectTargetDictionary);
      }
    }
    base.OnActionExecuting(filterContext);
}

我看到登录后我有两个cookie:

  1. ASPXAUTH(到期日期设置为“在会话结束时”(当持久为假时)或(从现在起 30 分钟(当持久设置为真)
  2. 和 ASP.NET_SessionId 过期时间总是“在会话结束时”

问题:问题是即使我将 TRUE 设置为“persists”选项(这将设置 ASPXAUTH 过期时间从现在开始 30 分钟 - 这很好),我的 Session["AdminId"] 在我关闭并重新打开浏览器后始终为空。当我最初将“persists”设置为true并关闭然后重新打开浏览器窗口时,如何确保从cookie中提取我的会话(Session [“AdminId”]和Session [“AdminUsername”])。谢谢

4

2 回答 2

0

具有到期时间的 cookie 被写入磁盘。因此,只要 cookie 没有过期,用户下次打开浏览器时仍会登录。

会话 cookie 仅存储在内存中,一旦关闭浏览器就会丢失。

会话 cookie 是没有到期日期的 cookie。

于 2011-01-13T22:31:47.137 回答
0

我在这里找到了我的解决方案:Is it possible to use .ASPXAUTH for my own logging system?

这就是我所做的:

    public class SessionExpireFilterAttribute : ActionFilterAttribute
{
    /// <summary>
    /// Controller action filter is used to check whether the session is still active. If the session has expired filter redirects to the login screen.
    /// </summary>
    /// <param name="filterContext"></param>
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var ctx = HttpContext.Current;

        // check if session is supported
        if (ctx.Session != null)
        {
            // check if a new session id was generated
            if (ctx.Session.IsNewSession)
            {
                var identity = ctx.User.Identity;

                // If it says it is a new session, but an existing cookie exists, then it must
                // have timed out
                string sessionCookie = ctx.Request.Headers["Cookie"];
                if (((null != sessionCookie) && (sessionCookie.IndexOf("ASP.NET_SessionId") >= 0)) || null == sessionCookie)
                {
                    var redirectTargetDictionary = new RouteValueDictionary();
                    redirectTargetDictionary.Add("area", string.Empty);
                    redirectTargetDictionary.Add("action", "LogOn");
                    redirectTargetDictionary.Add("controller", "User");

                    filterContext.Result = new RedirectToRouteResult(redirectTargetDictionary);
                }

                // Authenticated user, load session info
                else if (identity.IsAuthenticated)
                {
                    var loginRepository = new LoginRepository(InversionOfControl.Container.Resolve<IDbContext>());
                    IAuthenticationService authenticationService = new AuthenticationService(loginRepository);
                    authenticationService.SetLoginSession(identity.Name);
                }
            }
            else if (SessionContext.LoginId == null)
            {
                var redirectTargetDictionary = new RouteValueDictionary();
                redirectTargetDictionary.Add("area", string.Empty);
                redirectTargetDictionary.Add("action", "LogOn");
                redirectTargetDictionary.Add("controller", "User");

                filterContext.Result = new RedirectToRouteResult(redirectTargetDictionary);
            }
        }
        base.OnActionExecuting(filterContext);
    }
}
于 2011-01-14T12:12:47.540 回答