在页面加载之前加载cookie [英] Load cookie before page load

查看:92
本文介绍了在页面加载之前加载cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个加载cookie功能,它是为我的网站记住我功能而开发的。它工作正常,但有一个问题是,当网站第一次加载时,说最终用户设置了记住我部分然后登录到网站,使用后他/她关闭浏览器,他/她打开浏览器后然后尝试加载网站,cookie正在加载但是作为加载时间,cookie设置会话值,以便用户看到登录按钮,下次最终用户点击任何区域与第2页之后的网站进行交互加载用户看到他/她已登录,这是问题,是否有任何方法在页面加载之前处理cookie,以便在页面加载之前设置会话,用户可以看到他/她已登录第一次尝试



我尝试过:



=== ============== javascript代码======================

I have a load cookie function which is developed for "Remember me" functionality to my site. It is working fine but one issue is ,when the site is loading for first time , say end user has set the remember me section then logon to the site,after using it he/she close the browser, after he/she open the browser and then try to load the site ,the cookie is loading but as the loading time the cookie sets the session values so that the user gets to see log in button ,next time the end user clicks any area to interact with the site after 2nd page load the user gets to see he/she is logged on, this is the issue , Is there any way to process the cookie before the page loads so that it sets the session before page load and user can see he/she is logged on in the first attempts

What I have tried:

================= javascript code ======================

$(document).ready(function () {
    LoadCookies();
});
function LoadCookies() {
    $.ajax({
        type: "POST",
        url: "index.aspx/LoadCookie",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: onSuccess,
        failure: function (response) { alert("write log failure " + response.d); }
    });
    function onSuccess(response) {
        console.log(response.d);
    }
}



========================= =========================

=============== asp。网络代码======================


==================================================
=============== asp.net code======================

[System.Web.Services.WebMethod(EnableSession = true)]
        [System.Web.Script.Services.ScriptMethod()]
        public static void LoadCookie()
        {
            BidderBL objBidderBL = new BidderBL();
            if (HttpContext.Current.Request.Cookies["LoginCookie"] != null)
            {
                if (HttpContext.Current.Request.Cookies["LoginCookie"].Values["UserName"] != null)
                {
                    string Email = HttpContext.Current.Request.Cookies["LoginCookie"].Values["UserName"].Trim().ToString();
                    string Password = HttpContext.Current.Request.Cookies["LoginCookie"].Values["Password"].Trim().ToString();
                    string UserType = "1";
                    DataTable dtLogin = objBidderBL.GetLoginUser(Email, Password, UserType);
                    if (dtLogin != null && dtLogin.Rows.Count > 0)
                    {
                        HttpContext.Current.Session["UserType"] = "Bidder";
                        HttpContext.Current.Session["UserName"] = Convert.ToString(dtLogin.Rows[0]["PreferredName"]);
                        HttpContext.Current.Session["UserId"] = Convert.ToString(dtLogin.Rows[0]["BidderId"]);
                    }
                }
            }
        }

推荐答案

document )。ready( function (){
LoadCookies();
});
function LoadCookies(){
(document).ready(function () { LoadCookies(); }); function LoadCookies() {


.ajax({
type: POST
url: index.aspx / LoadCookie
contentType: application / json; charset = utf-8
dataType: json
成功:onSuccess,
失败: function (响应){alert( 写日志失败 + response.d);}
});
function onSuccess(response){
console .log(response.d) ;
}
}
.ajax({ type: "POST", url: "index.aspx/LoadCookie", contentType: "application/json; charset=utf-8", dataType: "json", success: onSuccess, failure: function (response) { alert("write log failure " + response.d); } }); function onSuccess(response) { console.log(response.d); } }



========================= =========================

=============== asp。网络代码======================


==================================================
=============== asp.net code======================

[System.Web.Services.WebMethod(EnableSession = true)]
        [System.Web.Script.Services.ScriptMethod()]
        public static void LoadCookie()
        {
            BidderBL objBidderBL = new BidderBL();
            if (HttpContext.Current.Request.Cookies["LoginCookie"] != null)
            {
                if (HttpContext.Current.Request.Cookies["LoginCookie"].Values["UserName"] != null)
                {
                    string Email = HttpContext.Current.Request.Cookies["LoginCookie"].Values["UserName"].Trim().ToString();
                    string Password = HttpContext.Current.Request.Cookies["LoginCookie"].Values["Password"].Trim().ToString();
                    string UserType = "1";
                    DataTable dtLogin = objBidderBL.GetLoginUser(Email, Password, UserType);
                    if (dtLogin != null && dtLogin.Rows.Count > 0)
                    {
                        HttpContext.Current.Session["UserType"] = "Bidder";
                        HttpContext.Current.Session["UserName"] = Convert.ToString(dtLogin.Rows[0]["PreferredName"]);
                        HttpContext.Current.Session["UserId"] = Convert.ToString(dtLogin.Rows[0]["BidderId"]);
                    }
                }
            }
        }


处理此问题的最简单方法是简单地重定向此人他们登录后因此,将它们重定向到他们所在的同一页面,或者重定向到我的帐户类型页面,这样网站就可以正常工作而无需进行太多修改。



另外,我希望你不是以明文形式将密码存储在cookie中吗?
The easiest way to handle this is to simply redirect the person after they login. So redirect them either to the same page they were on, or to a "my account" type page, such that the site works as normal without you having to modify much.

Also I hope you're not storing the password in the cookie in plain text?


这篇关于在页面加载之前加载cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆