Cookie会在每次会话后到期

时间:2015-04-28 04:55:32

标签: asp.net session asp.net-mvc-5

我的web.config我已经设置了10分钟超时

<sessionState mode="InProc" timeout="10" />

我的过滤器如下

public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        HttpSessionStateBase session = filterContext.HttpContext.Session;

        HttpCookie userInfoCookie =
            HttpContext.Current.Response.Cookies[System.Configuration.ConfigurationManager.AppSettings[Convert.ToString(CookieField.cookieName)]];
        string userExpire = string.Empty;
        userExpire = new UserControllerHelper().FetchDataFromCookie(CookieField.UserRoleId.ToString());
        if (userExpire == null) 
        { 
            filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary { { "controller", "Home" }, { "action", "Index" }, { "Area", "" } });
        }
    }

我的所有方法都有过滤器[SessionExpire] 它适用于一个会话,但是对于下一次单击相同的操作方法,cookie会自动清除。

在Global.asax中,我有以下代码

void Session_Start(object sender, EventArgs e)
{
    Session.Timeout = 10;            
}

void Session_End(object sender, EventArgs e)
{
}

我尝试了不同的时间,但仍然没有运气。

我使用以下代码保存Cookie

//create a cookie
        string cookieName = System.Configuration.ConfigurationManager.AppSettings[Convert.ToString(CookieField.cookieName)];
        HttpCookie userCookie = new HttpCookie(cookieName);
        //Add key-values in the cookie
        userCookie.Values.Add(Convert.ToString(CookieField.UserId), EncryptData(Convert.ToString(userRegistrationModel.Id)));
        userCookie.Values.Add(Convert.ToString(CookieField.UserFirstName), EncryptData(Convert.ToString(userRegistrationModel.FirstName)));
        userCookie.Values.Add(Convert.ToString(CookieField.UserLastName), EncryptData(Convert.ToString(userRegistrationModel.LastName)));
        userCookie.Values.Add(Convert.ToString(CookieField.UserLogin), EncryptData(Convert.ToString(userRegistrationModel.Login)));
        userCookie.Values.Add(Convert.ToString(CookieField.UserPassword), Convert.ToString(userRegistrationModel.LoginPassword));
        userCookie.Values.Add(Convert.ToString(CookieField.UserRoleId), EncryptData(Convert.ToString(userRegistrationModel.RoleId)));
        userCookie.Values.Add(Convert.ToString(CookieField.UserConfirmed), Convert.ToString(userRegistrationModel.IsConfirmedByAdmin));
        //set cookie expiry date-time, if required.
        userCookie.Expires = DateTime.Now.AddMinutes(10);
        //writing the cookie to client.
        HttpContext.Current.Response.Cookies.Add(userCookie);

但过了一会儿,“过期”会自动变为0。 在此代码中保存到期日期时是否有任何错误?

1 个答案:

答案 0 :(得分:0)

我更新了过滤器并解决了:)

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
    HttpSessionStateBase session = filterContext.HttpContext.Session;
    string userExpire = string.Empty;
    userExpire = new UserControllerHelper().FetchDataFromCookie(CookieField.UserRoleId.ToString());
    if (userExpire == null) 
    { 
        filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary { { "controller", "Home" }, { "action", "Index" }, { "Area", "" } });
    }
}