Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.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_Session_Login - Fatal编程技术网

C# 即使向服务器发送请求,会话也将过期

C# 即使向服务器发送请求,会话也将过期,c#,asp.net,session,login,C#,Asp.net,Session,Login,当用户登录时,我将其id存储在会话中,比如在session[“id”]中。在大多数页面上,我将会话id存储为整数,并在各种方法中使用它。我检查了页面加载事件 protected void Page_Load(object sender, EventArgs e) { if (Session["id"] == null) { Response.Redirect("Home.aspx"); } //code goes here } 我所知道的是,若并

当用户登录时,我将其id存储在会话中,比如在
session[“id”]
中。在大多数页面上,我将会话id存储为整数,并在各种方法中使用它。我检查了页面加载事件

protected void Page_Load(object sender, EventArgs e)
{
    if (Session["id"] == null)
    {
        Response.Redirect("Home.aspx");
    }
    //code goes here
}

我所知道的是,若并没有向服务器发送请求,会话将在20分钟后过期。但即使连续发送请求会话也会过期,我会在主页上重定向。这是正确的方法还是我应该尝试其他方法。任何帮助都将不胜感激。

正确的方法是使用透明地处理所有这些细节的。如中所示,您可以使用web.config中的参数直接在成员资格API中设置超时时间间隔


希望我能帮忙

如果对服务器的两个请求之间的间隔超过20分钟,则只有您的会话将过期

在ASP.NET中使用永久用户登录会话该示例介绍了如何在ASP.NET中创建永久用户登录会话。示例代码包括一个ASP.NET MVC4项目,用于控制用户注册和登录过程。但是,您可以在任何类型的ASP.NET项目中使用此技术。但简单地说,您可以使用此代码

此类的功能是将表单身份验证票证添加到浏览器cookie集合中,其有效期到期

public sealed class CookieHelper
{
private HttpRequestBase _request;
private HttpResponseBase _response;

public CookieHelper(HttpRequestBase request, 
HttpResponseBase response)
{
    _request = request;
    _response = response;
}

//[DebuggerStepThrough()]
public void SetLoginCookie(string userName,string password,bool isPermanentCookie)
{
    if (_response != null)
    {
        if (isPermanentCookie)
        {
            FormsAuthenticationTicket userAuthTicket = 
                new FormsAuthenticationTicket(1, userName, DateTime.Now, 
                DateTime.MaxValue, true, password,               FormsAuthentication.FormsCookiePath);
            string encUserAuthTicket = FormsAuthentication.Encrypt(userAuthTicket);
            HttpCookie userAuthCookie = new HttpCookie
                (FormsAuthentication.FormsCookieName, encUserAuthTicket);
            if (userAuthTicket.IsPersistent) userAuthCookie.Expires = 
                    userAuthTicket.Expiration;
            userAuthCookie.Path = FormsAuthentication.FormsCookiePath;
            _response.Cookies.Add(userAuthCookie);
        }
        else
        {
            FormsAuthentication.SetAuthCookie(userName, isPermanentCookie);
        }
    }
}
}
此功能用于登录页面或单击登录按钮进行控制。在附带的示例项目中,以下函数是在AccountController类中编写的。此函数用于验证用户的登录,然后将永久表单身份验证票证添加到浏览器中

private bool Login(string userName, string password,bool rememberMe)
{
if (Membership.ValidateUser(userName, password))
{
    CookieHelper newCookieHelper = 
    new CookieHelper(HttpContext.Request,HttpContext.Response);
    newCookieHelper.SetLoginCookie(userName, password, rememberMe);
    return true;
}
else
{
    return false;
}
} 

在webconfig文件中设置SessionTime out,然后trydefault会话超时为20分钟,但有时我会在1分钟后收到expire消息。您确定您使用的是相同的上下文吗?是的,非常确定您在应用程序中是否执行任何IO操作?感谢您回答pantelis,到目前为止,我使用的是基于角色的安全性。我读了它并尝试了我知道这个dhananjay我还提到了它我想知道的是我应该在会话中存储id以用于用户的内容页吗