ASP.Net学习笔记015--ASP.Net中使用Cookie

ASP.Net学习笔记015--ASP.Net中使用Cookie
表单数据欺骗:
原理跟收到欺骗短信一样,移动信号塔[基站],伪装的移动信号塔会屏蔽移动信号,并且
在信号范围内的手机会自动切换为接收伪装信号塔的信号,随后伪装信号塔
就可以模拟任意客服账号进行发送短信等等....比如:10086
95559
-----------------------------
银行余额也不能放在viewstate中,因为篡改viewstate,让服务器错误识别,很危险
----------------------------------------------------
如何存机密信息?故事:
账号是唯一标识:
账号和存折的区别,存折就像隐藏字段
如果自己在存折上改余额,然后去银行取钱的话,那是行不通的
所以余额不是存在客户端(存折上),而是存在银行的服务器上
--------------------------------------------------------------
网银:
唯一的标识就是卡号
客户端提交数据给网银,网银去数据库查,取多少钱等,然后更改服务器数据库信息
-------------------------------------------------------------------------
要把机密放到服务器,并区分不同访问者的私密信息,就要用唯一标识,账号
------------------------------------------------------------------------------
cookie就是每个人都有一个,而且每个人都是唯一的一个
自己不能看别人的cookie,所以,保存在cookie中是安全的
------------------------------
如何在asp.net设置cookie
浏览器每次向服务器请求的时候,除了把表单元素里的东西提交给服务器,同时也会把
和站点相关的所有cookie提交,是强制性的,cookie也是保存在浏览器端的,
而且浏览器会再每次请求的时候都会把和这个站点相关的cookie提交给服务器
,并将服务端返回的cookie更新回数据库,因此可以将信息保存在cookie中
然后在服务器端读取,修改
-------------------------------
看下面的例子:
在/WebSiteTest中
新建:
Cookie1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Cookie1.aspx.cs" Inherits="Cookie1" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:Button ID="Button1" runat="server" οnclick="Button1_Click" 
            Text="设置cookie" />
    
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    
    </div>
    </form>
</body>
</html>
---------------------------------------------
Cookie1.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class Cookie1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {




    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.SetCookie(new HttpCookie ("Color",TextBox1 .Text ));
        //在服务器对象Response中设置cookie然后,在客户端可以用$.cookie读出
        //这样写,会把服务器设置的cookie,更新到本地浏览器的cookie中
        //所以服务器除了返回http和html信息以外还会返回,经过修改的cookie,然后浏览器会把拿到的cookie,更新本地的cookie
    }
}
-----------------------------------------
访问下
http://localhost:53627/WebSiteTest/Cookie1.aspx
这时候从textbox中填入blue,点击按钮设置cookie
------------------------------------------------------
然后:新建:
Cookie读2.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Cookie读2.aspx.cs" Inherits="Cookie读2" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        <asp:Button ID="Button1" runat="server" οnclick="Button1_Click" Text="Button" />
    
    </div>
    </form>
</body>
</html>
----------------------------------
Cookie读2.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class Cookie读2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {


    }
    protected void Button1_Click(object sender, EventArgs e)
    {
     Label1.Text =   Request .Cookies ["Color"].Value ;//客户端读取cookie


    }
}
----------------
访问
http://localhost:53627/WebSiteTest/Cookie%E8%AF%BB2.aspx
点击按钮,可以看到获取的cookie是,前面设置的cookie
------------------------------------------------
看下一般的大型网站里的图片:
比如yahoo
都会把图片和内容分域名放,因为如果把图片和内容放到同一个域名下的话
当浏览器请求图片的时候,也会把该域名下的cookie都发到服务器,这样
很消耗流量的
-------------------
所以
www.yahoo.com里的图片路径都是:
www.yimg.com/a.gif
这样
www.daqi.com  7月28 关闭了
img.daqi.com
-------------------------
这样分开放
再比如:
http://www.chinaz.com/news/2015/0728/427788.shtml
http://upload.chinaz.com/2015/0728/1438073775529.jpg
图片地址
-------------------------------------
互联网优化案例:图片服务器和主站域名不一样,降低cookie流量的传输
--------------------------------------------------------------
cookie缺点:
把机密信息放在cookie中也是不安全的,因为cookie也是保存在客户端浏览器
的,并且不能存放大量数据,这时候需要用一个串号标识是否是某个人
---------------------------------------------------------------
下一个知识点:
先看下面程序:
/WebSiteTest中新建:
变量1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="变量1.aspx.cs" Inherits="变量1" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        <asp:Button ID="Button1" runat="server" οnclick="Button1_Click" Text="Button" />
    
    </div>
    </form>
</body>
</html>
-------------------------------------------------
变量1.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class 变量1 : System.Web.UI.Page
{
    private int i = 0;
    protected void Page_Load(object sender, EventArgs e)
    {


    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        i++;
        Label1.Text = i.ToString();


    }
}
-------------------------------
这时候访问:
http://localhost:53627/WebSiteTest/%E5%8F%98%E9%87%8F1.aspx
可以看到:
label的值,并没有一直自增,而是到了1就不在自增了
-----------------------------------------------------
为什么?
public partial class 变量1 : System.Web.UI.Page
选中System.Web.UI.Page右键,转到定义
namespace System.Web.UI
{
    // 摘要:
    //     表示从 ASP.NET Web 应用程序的宿主服务器请求的 .aspx 文件(又称为 Web 窗体页)。
    [DefaultEvent("Load")]
    [ToolboxItem(false)]
    [Designer("Microsoft.VisualStudio.Web.WebForms.WebFormDesigner, Microsoft.VisualStudio.Web, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(IRootDesigner))]
    [DesignerSerializer("Microsoft.VisualStudio.Web.WebForms.WebFormCodeDomSerializer, Microsoft.VisualStudio.Web, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.ComponentModel.Design.Serialization.TypeCodeDomSerializer, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
    [DesignerCategory("ASPXCodeBehind")]


    public class Page : TemplateControl, IHttpHandler


    可以看到ui.page这个类,实现了IHttpHandler这个接口
  ----------------------------------------------------------
回去看咱们写的一般处理程序:
IncValue2.ashx
public class IncValue2 : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/html";
      string ispostback =context.Request["ispostback"];
      string value = "0";
      if (ispostback =="true") {
          //这个时候是取不出值的
      value=context.Request["num1"];//这个时候是取不到值
          //注意,不是服务器读取网页,而是浏览器收集在表单中填写的字段,然后提交数据,
          //给服务器来处理,由于没有把div当前的innertext发给服务器,所以服务器不能得知当前的值
          //也不要幻想有办法能将div的innertext提交给服务器,因为只有设定了name的input,textarea
          //select的value属性值才会被提交给服务器,只针对前面input,textarea,select这几个控件才能提交数据


      int i = Convert.ToInt32(value);
      i++;
      value = Convert.ToString(i);
      }
      ...............
---------------------------------------------
: IHttpHandler {可以看到也是实现了: IHttpHandler 这个接口,也就是说所有的服务端的处理程序,都实现了: IHttpHandler 接口,
一般处理程序,直接实现了: IHttpHandler 接口,而aspx通过
    [DesignerCategory("ASPXCodeBehind")]
    public class Page : TemplateControl, IHttpHandler
    继承TemplateControl 这个,
        //     为 System.Web.UI.Page 类和 System.Web.UI.UserControl 类提供一组基本功能。
    public abstract class TemplateControl : Control, INamingContainer, IFilterResolutionService
    继承TemplateControl又继承Control在这里帮助咱们做了很多东西
-----------------------------------------------------------------
这样可以总结,aspx也是一般处理程序,只不过是特殊的一般处理程序,它的父类帮助咱们做了很多工作而已
------------------------------------------------------------------------
上面的问题得以解决:
当客户端发请求的时候:服务器会创建一个handler对象
IHttpHandler handler =new IncValue2();
handler.ProcessRequest(.....);
请求完之后就被回收了,下次再请求又从新建了这样一个handler对象
也就是每次请求都会从新建一个这样的对象,所以上面的变量,并没有自增
----------------------------------------------------------------
下面写一个可以自增的:
变量1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="变量1.aspx.cs" Inherits="变量1" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        <asp:Button ID="Button1" runat="server" οnclick="Button1_Click" Text="Button" />
    
    </div>
    </form>
</body>
</html>
---------------------------------------------
变量1.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class 变量1 : System.Web.UI.Page
{
    private int i = 0;//每次请求来了都会new一个新的实现了IHttpHandler接口的类,变量1的实例
    //进行处理,用完了就回收了,所以不会保持上次的值.
   private static int j=0;
    protected void Page_Load(object sender, EventArgs e)
    {


    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //i++;
        //Label1.Text = i.ToString();
        //可以看到设置成静态的就可以自增了,因为static常住内存,全局变量
          j++;
        Label1.Text = j.ToString();
    }
}
-----------------------------------------------------------
访问:
http://localhost:53627/WebSiteTest/%E5%8F%98%E9%87%8F1.aspx
可以看到j可以自增了,这是因为使用了全局变量的原因
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

添柴程序猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值