How to reset ASP.Net sessions state timeout with ajax request

隐身守侯 提交于 2019-12-07 10:29:20

问题


I am working on an ASP.Net Full-Ajax site. Because all operations have doing by Ajax requests, any postback request will not send to server.In other hand I have a javascript timer that send an ajax request to check sessions and if it expired redirect user to login page.

Problem is here: When user is working on page for 20 minutes and all operations will do with ajax requests after 20 minutes its session expired and user will redirect to login page (by that javascript timer) exactly during working with page. So I need to refresh its session state with ajax request. But how ?How can I do that to reset sessions state timeout by an ajax request!?!!

Depend on my Google search results, I can not be preform by ajax request because SessionId store in client as a cookie and to update it need to preform a post back request :(

Note : Session state is set on InProc mode with timeout = 20

Sorry about my bad English syntax, I am new in English

Regards, Foroughi

UPDATE : Does an ajax request update sessions state timeout?!!

UPDATE : When my user login to site I set a session like this :

Session["UserId"] = UserObject.Id;

and in all my page I use some web method to preform operation like this :

[WebMethod]
public static Opr1 (Paramethers...)
{

   //Question is here , how can i update UserId  session to prevent to expire,how can i update it

   //execute my codes to preform Opr1

}

回答1:


If you are using WebMethods you should decorate your methods like

[WebMethod(EnableSession = true)]

Further if you need to keep your session alive you should try create an HTTPHandler that implements IRequireSessionState this interface allows to get/set(deserialize/serialize) the session variables that will eventually slide session timeout.

This is a good article about the sessions and ajax calls.

http://seejoelprogram.wordpress.com/2008/11/10/maintaining-aspnet-session-state-in-an-ajax-application/

Hope this will be helpful

Regards.




回答2:


this is the way I am doing:

I have a simple web service to check if user is authenticated, but you can change the code to check if session is not expired.

<%@ WebService Language="C#" Class="CheckAutheticated" %>

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

[System.Web.Script.Services.ScriptService]
public class CheckAutheticated  : System.Web.Services.WebService {

[WebMethod]
public string checkAuthenticated()
{
    return "authenticated";
}

}

then client side I call it:

function checkAuthenticated() {
        {
            $.ajax({
                type: "POST",
                url: "CheckAutheticated.asmx/checkAuthenticated",
                data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: checkAuthenticatedOk,
                error: checkAuthenticatedError
            });
        }
    }
    function checkAuthenticatedOk() { }
    function checkAuthenticatedError() {
        $("#sessionExpiredCover").show();
    }

here some basic css

<style type="text/css">
    #sessionExpiredCover {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 100000;
    display: none;
    background-color: #fff;
    /*opacity: .7;
    filter:progid:DXImageTransform.Microsoft.BasicImage(opacity=.7);*/
    }
    </style>

and finally the div

    <div id="sessionExpiredCover">
    <div style="background-color:#fff; margin:100px; height:200px; width:400px;"><h1>Session expired</h1>
        <br />
        <asp:HyperLink NavigateUrl="~/Login.aspx" runat="server" Text="Please log in again" />
    </div>
    </div>


来源:https://stackoverflow.com/questions/9374190/how-to-reset-asp-net-sessions-state-timeout-with-ajax-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!