ASP.NET中使用验证码技术
由于验证码技术中服务器程序需要创建验证码图片,里面用到了图形编程,因此本节课程仍然是C#发现之旅的图形编程系列教程。
根据验证码的原理,我们使用C#在ASP.NET中实现了验证码的功能。
checkimage.aspx
首先根据上节课程的内容,我们要创建一个图片服务页面,专门用于提供包含验证码文本的图片,为此我们建立一个 checkimage.aspx 的页面。其HTML代码很简单,只有一行,不输出任何内容。在其Page_Load方法中就有创建验证码图片的过程。
// 创建一个包含随机内容的验证码文本
System.Random rand = new Random();
int len = rand.Next(4 , 6 );
char[] chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
System.Text.StringBuilder myStr = new System.Text.StringBuilder();
for( int iCount = 0 ; iCount < len ; iCount ++ )
{
myStr.Append( chars[ rand.Next( chars.Length )]);
}
string text = myStr.ToString();
// 保存验证码到 session 中以便其他模块使用
this.Session["checkcode"] = text ;
Size ImageSize = Size.Empty ;
Font myFont = new Font("MS Sans Serif" , 20 );
// 计算验证码图片大小
using( Bitmap bmp = new Bitmap( 10 , 10 ))
{
using( Graphics g = Graphics.fromImage( bmp ))
{
SizeF size = g.MeasureString( text , myFont , 10000 );
ImageSize.Width = ( int ) size.Width + 8 ;
ImageSize.Height = ( int ) size.Height + 8 ;
}
}
// 创建验证码图片
using( Bitmap bmp = new Bitmap( ImageSize.Width , ImageSize.Height ))
{
// 绘制验证码文本
using( Graphics g = Graphics.fromImage( bmp ))
{
g.Clear( Color.White );
using( StringFormat f = new StringFormat())
{
&
相关文档:
验证码应用:将代码复制到页面即可,并将页面的路径写到图片的url中便可使用
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebCont ......
protected void Button1_Click(object sender, EventArgs e)
{
string fullName = this.File1.PostedFile.FileName;//获取上传文件的全路径
string size = th ......
asp.net自定义错误处理页面方法一
1、添加Web.config, < system.web>< /system.web>中添加< customErrors mode="On"
defaultRedirect="ApplicationErroy.aspx" >< /customErrors>节点,
2、添加错误处理页面:ApplicationErroy.aspx调用下面的方法:
private void DealErroy() & ......
十、如何:读取 Calendar Web 服务器控件中的选定日期
第一个示例显示当前选定的日。第二个示例显示所有选定的日期,可以是单个日、周或月。
// Example 1
Label1.Text = Calendar1.SelectedDate.ToShortDateString();
// Example 2
String s = "";
foreach(DateTime d in Ca ......