欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net 操作cookie的简单实例

程序员文章站 2024-02-25 22:57:09
复制代码 代码如下: protected void page_load(object sender, eventargs e)   ...

复制代码 代码如下:

 protected void page_load(object sender, eventargs e)
    {
        if (!ispostback)
        {
            httpcookie cookie = request.cookies["logininfo"];

            if (cookie != null)
            {
                cb_repwd.checked = true;
                tb_loginname.text = uihelper.decrypt(cookie["loginname"]);
                tb_pwd.attributes.add("value", uihelper.decrypt(cookie["pwd"]));
            }
            else
            {
                tb_pwd.attributes.add("value", "******");
            }

        }

    }

    protected void btn_login_click(object sender, eventargs e)
    {

        if (uihelper.getstring(session["code"]).toupper()!=this.tb_code.text.trim().toupper())
        {
            ojavascript.alert("验证码输入不正确");
            return;
        }

        netuserservice nerservice = contextfactory.getobject<netuserservice>("netuserservice");
        string sql = string.format("from netuser where loginname='{0}'", tb_loginname.text.trim());
        ilist<netuser> list = nerservice.find(sql);
        if (list.count > 0)
        {
            if (list[0].status == 2)
            {
                ojavascript.alert("帐号已停用,请联系管理员");
                return;
            }
        }

        if (nerservice.login(tb_loginname.text.trim(), osecurity.getpwdencrypt(tb_pwd.text.trim())))
        {
            httpcookie cookie = request.cookies["logininfo"];
            if (cb_repwd.checked)
            {
                if (cookie == null)
                {
                    httpcookie cookielogin = new httpcookie("logininfo");
                    setcookies(cookielogin,7);
                }
                else
                {
                    if (uihelper.decrypt(request.cookies["logininfo"]["loginname"]) != tb_loginname.text.trim())
                    {
                        setcookies(cookie, 7);
                    }
                }
            }
            else
            {
                setcookieexpires(cookie);
            }

           
            ojavascript.redirect("/");
        }
        else
        {
            ojavascript.alert( "用户名或密码不正确");
        }
    }

    #region cookie
    /// <summary>
    /// 设置cookie
    /// </summary>
    /// <param name="cookielogin">httpcookie cookielogin</param>
    /// <param name="expires">有效期</param>
    private void setcookies(httpcookie cookielogin, int expires)
    {
        cookielogin.values["loginname"] = uihelper.encrypt(tb_loginname.text.trim());
        cookielogin.values["pwd"] = uihelper.encrypt(tb_pwd.text.trim());
        cookielogin.expires = datetime.now.adddays(expires);
        response.appendcookie(cookielogin);
    }

    /// <summary>
    /// 设置cookie失效
    /// </summary>
    /// <param name="cookielogin">httpcookie cookielogin</param>
    private void setcookieexpires(httpcookie cookielogin)
    {
        if (cookielogin != null)
        {
            cookielogin.expires = datetime.now.adddays(-1); ;
            response.appendcookie(cookielogin);
        }
    }
    #endregion

设置cookie过期其实就是修改cookie,必须重新向客户端发送cookie

request.cookies取cookie, response,cookies添加cookie

虽然简单,但是不太了解的还是很容易出错