• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

asp.net 操作cookie的简单实例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

复制代码 代码如下:

 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

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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
asp.net数据绑定DataBind使用方法发布时间:2022-02-05
下一篇:
asp.net生成缩略图示例方法分享发布时间:2022-02-05
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap