现在的位置: 首页 > 综合 > 正文

缓存引起的session问题(ajax,js,jquery,asp.net,C#)

2013年09月12日 ⁄ 综合 ⁄ 共 2948字 ⁄ 字号 评论关闭

AsyncData.ashx文件

public void ProcessRequest (HttpContext context) {
        string action = context.Request.QueryString["action"];//getcity,getstore
        string arg = context.Request.QueryString["arg"];// areaGUID,
        string pwd = context.Request.QueryString["pwd"];//user login password
        StringBuilder strResult = new StringBuilder();
       
        if (action == "userLogin")
        {
            Model.BJ_UserInfo userModel = new Model.BJ_UserInfo();
            BLL.BJ_User userBll = new BLL.BJ_User();
            UserInfo ui = new UserInfo();
           
            if (string.IsNullOrEmpty(arg) || string.IsNullOrEmpty(pwd))
                return;
            else
                userModel = userBll.GetModel(arg, WebUtility.GetMD5(pwd));
            if (userModel != null)
            {
                ui.UserGUID = userModel.UserGUID.ToString();
                ui.UserName = userModel.UserName;
                ui.RealName = userModel.RealName;
                ui.SecurityGUID = userModel.SecurityGUID.ToString();
                //ui.IP = WebUtility.GetIpAddress;
                //ui.StoreName = IpLocation(ui.IP);
                context.Session["userinfo"] = ui;
                strResult.Append(ui.UserName);
            }
            else
                strResult.AppendFormat("0");
        }
       }

 

CheckLogin.js文件

//---------------------javascript-------------------------------
jQuery(function($){
   
    $("#btnLogin").click(function(){        
        var target1 = $("#txtUserName");
        var target2 = $("#txtPwd");   
        if(target1.val()=='')
        {
            alert('用户名不能为空!');
            return;
        }
        if(target2.val()=='')
        {
            alert('密码不能为空!');
            return;
        }
        else
           
            funAjaxText("userLogin", target1.val(), target2.val());           
    });
   
    var funAjaxText = function(action, username,pwd){
        $.ajax({
            type: "GET",
            url: "../Controls/AsyncData.ashx?action="+ action +"&arg="+username+"&pwd="+pwd,
            beforeSend: function(data){
            },
            success: function(data){            
                    if(data=="0")
                    {
                        $("#txtPwd").val("");
                        alert("用户名或密码有误!");                       
                    }
                    else
                    {                       
                        window.location.href="MyBJ/MyBJ.aspx";
                    }                               
                }           
        });
    };   
});

首页用户登录,进行退出时,清空当前会话

Session.Clear();
        Session.Abandon();
        Response.Redirect("index.aspx");

并跳转到前页(刷新),

再次登录时,登录成功,跳转到mybj.aspx页面

page_load()

{

UserInfo userModel = new UserInfo();
        userModel = (UserInfo)Session["userinfo"];
        if (userModel == null)
        {
            WebUtility.ShowMessageAndRedirect("你还没有登录!","../index.aspx", this.Page);           
        }

}

会出现,session["userinfo"]为空的情况。

这里的问题是缓存,

解决方法是在进行异步数据请求时,加多一个随机数,以使此请求不同于前,于是才不会去读取缓存。

url: "../Controls/AsyncData.ashx?action="+ action +"&arg="+username+"&pwd="+pwd+"&update="+Math.random(),

 

抱歉!评论已关闭.