关键词搜索

源码搜索 ×
×

c#.net与vb.net中读写Cookie的方法!

发布2021-02-09浏览434次

详情内容

Cookie (HttpCookie的实例)提供了一种在 Web 应用程序中存储用户特定信息的方法。例如,当用户访问您的站点时,您可以使用 Cookie 存储用户首选项或其他信息。当该用户再次访问您的网站时,应用程序便可以检索以前存储的信息。

C#.net部分

创建Cookie方法 (1)

Response.Cookies["userName"].Value = “admin";
Response.Cookies[“userName”].Expires = DateTime.Now.AddDays(1);

    //如果不设置失效时间,Cookie信息不会写到用户硬盘,浏览器关闭将会丢弃。

    创建Cookie方法 (2)

    HttpCookie aCookie = new HttpCookie(“lastVisit”); //上一次访问时间
    aCookie.Value = DateTime.Now.ToString();
    aCookie.Expires = DateTime.Now.AddDays(1);
    Response.Cookies.Add(aCookie);
    
      3
    • 4

    访问Cookie方法(1)

    if(Request.Cookies["userName"] != null)
    Label1.Text = Server.HtmlEncode(Request.Cookies["userName"].Value);访问Cookie方法(2)
    if(Request.Cookies["userName"] != null)
    {
    HttpCookie aCookie = Request.Cookies["userName"];
    Label1.Text = Server.HtmlEncode(aCookie.Value);
    }
    
      3
    • 4
    • 5
    • 6
    • 7

    创建多值Cookie方法 (1)

    Response.Cookies["userInfo"]["userName"] = “admin";
    Response.Cookies["userInfo"]["lastVisit"] = DateTime.Now.ToString();
    Response.Cookies["userInfo"].Expires = DateTime.Now.AddDays(1);
    
      3

    创建多值Cookie方法 (2)

    HttpCookie aCookie = new HttpCookie("userInfo");
    aCookie.Values["userName"] = “admin";
    aCookie.Values["lastVisit"] = DateTime.Now.ToString();
    aCookie.Expires = DateTime.Now.AddDays(1);
    Response.Cookies.Add(aCookie);
    
      3
    • 4
    • 5

    读取多值Cookie

    HttpCookie aCookie = Request.Cookies["userInfo"];
    string userName=aCookie.Values[“userName”];
    string lastVisit=aCookie.Values[“lastVisit”];
    
      3

    vb.net部分

    创建Cookie方法 (1)

    Response.Cookies("userName").Value = “admin"
    Response.Cookies(“userName”).Expires = DateTime.Now.AddDays(1)
    

      '如果不设置失效时间,Cookie信息不会写到用户硬盘,浏览器关闭将会丢弃。

      创建Cookie方法 (2)

      Dim aCookie As HttpCookie =  New HttpCookie(“lastVisit”)  '上一次访问时间
      aCookie.Value = DateTime.Now.ToString()
      aCookie.Expires = DateTime.Now.AddDays(1)
      Response.Cookies.Add(aCookie)
      
        3
      • 4

      访问Cookie方法(1)

      If Not Request.Cookies("userName") Is Nothing Then
      Label1.Text = Server.HtmlEncode(Request.Cookies("userName").Value)访问Cookie方法(2)
      End If
      If Not Request.Cookies("userName") Is Nothing Then
      Dim aCookie As HttpCookie =  Request.Cookies("userName")
      Label1.Text = Server.HtmlEncode(aCookie.Value)
      End If
      
        3
      • 4
      • 5
      • 6
      • 7

      创建多值Cookie方法 (1)

      Response.Cookies("userInfo")("userName") = “admin"
      Response.Cookies("userInfo")("lastVisit") = DateTime.Now.ToString()
      Response.Cookies("userInfo").Expires = DateTime.Now.AddDays(1)
      
        3

      创建多值Cookie方法 (2)

      Dim aCookie As HttpCookie =  New HttpCookie("userInfo")
      aCookie.Values("userName") = “admin"
      aCookie.Values("lastVisit") = DateTime.Now.ToString()
      aCookie.Expires = DateTime.Now.AddDays(1)
      Response.Cookies.Add(aCookie)
      
        3
      • 4
      • 5

      读取多值Cookie

      Dim aCookie As HttpCookie =  Request.Cookies("userInfo")
      Dim userName As String = aCookie.Values(“userName”)
      Dim lastVisit As String = aCookie.Values(“lastVisit”)
      
        3

      修改和删除Cookie

      不能直接修改或删除Cookie,只能创建一个新vb.net教程的Cookie,发送到客户端以实现修改或删除Cookie.

      转载地址 http://www.cqkalai.com/NewsView/?ID=361&SortID=5

      相关技术文章

      点击QQ咨询
      开通会员
      返回顶部
      ×
      微信扫码支付
      微信扫码支付
      确定支付下载
      请使用微信描二维码支付
      ×

      提示信息

      ×

      选择支付方式

      • 微信支付
      • 支付宝付款
      确定支付下载