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可以自增了,这是因为使用了全局变量的原因

ASP.Net学习笔记015--ASP.Net中使用Cookie相关推荐

  1. asp.net学习笔记异常处理001---.framework4.0中asp.net页面ValidateRequest=false 无效的问题

    在做牛腩新闻发布系统的时候,部分同学可能会遇到这样的情况: 从客户端(ContentPlaceHolder1_m_ContentPlaceHolder_ftbContent="<P&g ...

  2. ASP.NET学习笔记(11)--ASP简介

    一般来说,ASP包括5个对象:Request,Response,,Server,Session,Application.在html代码中,[%...%]表明这是ASP代码. 1,Request对象: ...

  3. ASP.Net学习笔记014--ViewState初探3

    ASP.Net学习笔记014--ViewState初探3 为什么禁用了viewstate,还能修改label2的值 因为:viewstate只是记录label2的值,并不影响给label2进行设置 - ...

  4. ASP.Net学习笔记013--ViewState初探2

    ASP.Net学习笔记013--ViewState初探2 上课讲的viewstate,由于需要跟后台服务器进行传值,需要封装很多隐藏列,比如100条数据,就会有100个viewstate 如果用在一些 ...

  5. ASP.Net学习笔记008--ASP.Net揭秘之Input版自增补充说明

    以前写的课程都没有附上源码,很抱歉! ASP.Net学习笔记007ASP.Net Input版自增.zip http://credream.7958.com/down_20155694.html 1. ...

  6. ASP.Net学习笔记007--ASP.Net Input版自增

    2016/1/18 以前写的课程都没有附上源码,很抱歉! 课程中的源码可以加qq索要:1606841559 技术交流qq1群:251572072 技术交流qq2群:170933152 也可以自己下载: ...

  7. ASP.Net学习笔记006--Get和Post的区别

    以前写的课程都没有附上源码,很抱歉! 课程中的源码可以加qq索要:1606841559 技术交流qq1群:251572072 技术交流qq2群:170933152 也可以自己下载: ASP.Net学习 ...

  8. ASP.Net学习笔记005--ASP.Net的IsPostBack揭秘

    以前写的课程都没有附上源码,很抱歉! 课程中的源码可以加qq索要:1606841559 技术交流qq1群:251572072 技术交流qq2群:170933152 也可以自己下载: ASP.Net学习 ...

  9. ASP.Net学习笔记004--基于ashx方式的ASP.Net开发1

    以前写的课程都没有附上源码,很抱歉! 课程中的源码可以加qq索要:1606841559 技术交流qq1群:251572072 技术交流qq2群:170933152 也可以自己下载: ASP.Net学习 ...

最新文章

  1. 百度DisConf分布式配置框架源码试读(一)HttpClient 长连接
  2. python使用matplotlib可视化subplots子图、subplots绘制子图、子图之间有重叠问题、使用subplots_adjust函数合理设置子图之间的水平和垂直距离
  3. jUnit生命周期管理学习
  4. 在anaconda中运行jupyter notebook,无法自动打开浏览器的解决方案,亲测100%有效
  5. [2021.1.31多校省选模拟12]随机变换的子串(线段树维护分治/字符串/自动机思想)
  6. 韩顺平php视频笔记71 面向对象编程的三大特征2 访问修饰符 继承
  7. 中国物联网激荡 20 年
  8. DOS批处理中对含有特殊字符的文件名的处理方法
  9. SharePoint2013更改网站集端口方法
  10. ES6语法及vue复习
  11. PyTorch代码学习-ImageNET训练
  12. php 将格式化时间转化为时间戳 以及数据库中将格式化时间转化为时间戳
  13. 代码执行sql出错:SQL syntax error, expected token is ‘RIGHT_PAREN‘, actual token is ‘IDENTIFIER‘
  14. Linux - 第11节 - 网络入门
  15. win7中显示桌面的方式有哪些?
  16. Linux开机自启的三种方式
  17. 网络爬虫十三-队列处理
  18. HTML实现好看的登录注册页面
  19. 四、中英翻译、歌词、藏头诗、智能聊天
  20. 如何用深度学习处理结构化数据?

热门文章

  1. R-CNN家族梳理:从R-CNN到Mask R-CNN
  2. Hadoop2——SSH免密登录功能配置
  3. linux下无权限安装opencv3.4.6
  4. pytorch: 自定义数据集加载
  5. SpringBoot 手写过滤器amp;加载第三方过滤器
  6. jquery 随楼层滚动导航激活状态改变特效
  7. Spring官网下载dist.zip的几种方法
  8. RAID磁盘阵列配置和调优小结
  9. (转载)C#提取汉字拼音首字母的方法
  10. SC2012 Orchestrator - 文档及资源链接