ASP.NET 学习笔记_04 Session、http、web开发原则、xss漏洞

本文介绍了一种基于ASP.NET的验证码实现方法,通过C#代码生成包含加法运算的图片验证码,并将其结果存储在Session中以供验证。此外还涉及了HTTP协议的基本概念、Web开发的原则以及如何防范XSS攻击等实用技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、Session

2、验证码

YZM.ashx

<%@ WebHandler Language="C#" Class="YZM" %>

using System;
using System.Web;

public class YZM : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "image/JPEG";
        using (System.Drawing.Bitmap bitImage = new System.Drawing.Bitmap(130, 100))
        {
            //设置画布
            using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitImage))
            {

                //随机数字
                Random my_random = new Random();
                int num_01 = my_random.Next(100);
                int num_02 = my_random.Next(100);
                
                int result = num_01 + num_02;
                string num_string01 = num_01.ToString();
                string num_string02 = num_02.ToString();
                string result_string = result.ToString();
                //保存到服务器的sessionid中
                HttpContext.Current.Session["YZM"] = result_string;
                //设置文字
                g.DrawString(num_string01 +"+"+ num_string02+"?", new System.Drawing.Font("宋体", 20), System.Drawing.Brushes.Red, new System.Drawing.PointF(0, 0));
                //保存到输出流中
                bitImage.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            }
        }
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}

YZM.aspx

<form id="form1" runat="server">
    <div>
    <%--单击刷新验证码--%>
        <img src="YZM.ashx" alt="请输入验证码" onclick="this.src='YZM.ashx?aaa='+new Date()" />
    </div>
    <asp:TextBox ID="TextBox1" runat="server" text=""></asp:TextBox>
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
    </form>

3、http协议

4、按钮实现标哥行删除效果   超链接提交表单

5、web开发原则

6、XSS漏洞  

  label类似控件:Literal控件,如果 属性中text的值为<a href="www.baidu.com">hah</a>可以会被嵌入链接,

  可以设置mode属性为Encode

 7、虚拟目录~  :ASP.net中使用,始终在应用程序根目录下开始

 

 

转载于:https://www.cnblogs.com/daomul/archive/2013/03/30/2990229.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值