Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 登录会话永不过期_C#_Asp.net_Cookies_Forms Authentication - Fatal编程技术网

C# 登录会话永不过期

C# 登录会话永不过期,c#,asp.net,cookies,forms-authentication,C#,Asp.net,Cookies,Forms Authentication,在登录过程中,我正在我的AccountController中创建一个HttpCookie,如下所示 FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe); 当model.RememberMe为false时,我注意到即使重新启动浏览器,我也从未注销。会话不应该在20分钟后或在关闭浏览器时过期吗 在身份验证过程中,我还使用设置cookie来存储角色信息。这还设置了一个非持久性cookie。这是否会影响登录cookie

在登录过程中,我正在我的
AccountController
中创建一个HttpCookie,如下所示

FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
model.RememberMe
false
时,我注意到即使重新启动浏览器,我也从未注销。会话不应该在20分钟后或在关闭浏览器时过期吗

在身份验证过程中,我还使用设置cookie来存储角色信息。这还设置了一个非持久性cookie。这是否会影响登录cookie

我注意到的是,当我在
Global.asax.cs
中反序列化我的cookie时,
HttpCookie
的过期时间为
DateTime.MinValue
,而
authtticket
的过期时间未来最多为20分钟。如果我让这段时间过去,我没有注意到用户注销,事实上,随着到期日期在未来20分钟左右跳跃,有东西更新了cookie-但我在代码中看不到这种情况发生

有人能解释一下发生了什么事吗

    protected void Application_PostAuthenticateRequest(object sender, EventArgs e)
    {
        HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];

        if (authCookie != null)
        {
            var authTicket = FormsAuthentication.Decrypt(authCookie.Value);
            var serializer = new JavaScriptSerializer();
            var serializeModel = serializer.Deserialize<EMPrincipalSerializeModel>(authTicket.UserData);

            if (!authTicket.Expired && serializeModel != null && HttpContext.Current.User.Identity.IsAuthenticated)
            {
                var newUser = new EMPrincipal(authTicket.Name);
                newUser.Id = serializeModel.Id;
                newUser.RealName = serializeModel.RealName;
                newUser.Username = serializeModel.UserName;

                HttpContext.Current.User = newUser;
            }
        }
    }
受保护的无效应用程序\u PostAuthenticateRequest(对象发送方,事件参数e)
{
HttpCookie authCookie=Request.Cookies[FormsAuthentication.formscookeName];
if(authCookie!=null)
{
var authTicket=FormsAuthentication.Decrypt(authCookie.Value);
var serializer=新的JavaScriptSerializer();
var serializeModel=serializer.Deserialize(authTicket.UserData);
如果(!authtticket.Expired&&serializeModel!=null&&HttpContext.Current.User.Identity.IsAuthenticated)
{
var newUser=newemprincipal(authTicket.Name);
newUser.Id=serializeModel.Id;
newUser.RealName=serializeModel.RealName;
newUser.Username=serializeModel.Username;
HttpContext.Current.User=newUser;
}
}
}

由于滑动过期,您的身份验证过期时间正在更改,这意味着每次请求的过期时间都将在将来重置为x分钟。这是FormsAuthentication的默认行为。身份验证将仅在x分钟不活动后过期,每次调用请求检查会话时,您都在续订会话

如果愿意,您可以禁用此功能:

如果禁用,身份验证将在创建后x分钟过期,而不是在不活动后x分钟过期