网站登录验证码和小论坛程序[含源程序+数据库]
网站登录验证码和小论坛程序[含源程序+数据库]
有兴趣的朋友可以看看
下载JumpyForum
MD5加密方法:
有兴趣的朋友可以看看
下载JumpyForum
MD5加密方法:
using System.Security;
using System.Security.Principal;
using System.Security.Cryptography;
using System.Text;
/**//// <summary>
/// 加密函数
/// </summary>
public string Encrypt(string password)
{
/**////获取Byte数组
Byte[] clearBytes = new UnicodeEncoding().GetBytes(password);
/**////获取Hash值
Byte[] hashedBytes = ((HashAlgorithm)CryptoConfig.CreateFromName("MD5")).ComputeHash(clearBytes);
/**////获取加密后的信息
return BitConverter.ToString(hashedBytes);
}
验证码生成程序:using System.Security.Principal;
using System.Security.Cryptography;
using System.Text;
/**//// <summary>
/// 加密函数
/// </summary>
public string Encrypt(string password)
{
/**////获取Byte数组
Byte[] clearBytes = new UnicodeEncoding().GetBytes(password);
/**////获取Hash值
Byte[] hashedBytes = ((HashAlgorithm)CryptoConfig.CreateFromName("MD5")).ComputeHash(clearBytes);
/**////获取加密后的信息
return BitConverter.ToString(hashedBytes);
}
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.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Imaging;
using Security;
namespace Security
{
public partial class ValidateCode : System.Web.UI.Page
{
/// <summary>
/// 验证码长度
/// </summary>
private int codeLen = 6;
/// <summary>
/// 图片清晰度
/// </summary>
private int fineness = 85;
// 图片宽度
private int imgWidth = 80;
// 图片高度
private int imgHeight = 24;
// 字体家族名称
private string fontFamily = "Times New Roman";
// 字体大小
private int fontSize = 14;
// 字体样式
private int fontStyle = 0;
// 绘制起始坐标 X
private int posX = 0;
// 绘制起始坐标 Y
private int posY = 0;
protected void Page_Load(object sender, EventArgs e)
{
string validateCode = CreateValidateCode();//生成验证码
Bitmap bitmap = new Bitmap(imgWidth, imgHeight);// 生成BITMAP图像
DisturbBitmap(bitmap);// 图像背景
DrawValidateCode(bitmap, validateCode);// 绘制验证码图像
bitmap.Save(Response.OutputStream,ImageFormat.Gif);// 保存验证码图像,等待输出
}
/// <summary>
/// 生成验证码
/// </summary>
/// <returns></returns>
private string CreateValidateCode()
{
string validateCode = "";
string[] str =
{
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"
};
Random random = new Random();// 随机数对象
int n = 0;
for (int i = 0; i < codeLen; i++)
{
n = random.Next(str.Length);
validateCode += str[n];
}
this.strValidate = validateCode;// 保存验证码
return validateCode;
}
/// <summary>
/// 图像背景
/// </summary>
/// <param name="bitmap"></param>
private void DisturbBitmap(Bitmap bitmap)
{
Random random = new Random();// 通过随机数生成
for (int i = 0; i < bitmap.Width; i++)
{
for (int j = 0; j < bitmap.Height; j++)
{
if (random.Next(90) <= this.fineness)
bitmap.SetPixel(i, j, Color.White);
}
}
}
/// <summary>
/// 绘制验证码图像,bitmap 图板,validateCode 验证码值
/// </summary>
/// <param name="bitmap"></param>
/// <param name="validateCode"></param>
private void DrawValidateCode(Bitmap bitmap, string validateCode)
{
Graphics g = Graphics.FromImage(bitmap);// 获取绘制器对象
Font font = new Font(fontFamily, fontSize, FontStyle.Bold);// 设置绘制字体
g.DrawString(validateCode, font, Brushes.Black, posX, posY);// 绘制验证码图像
}
/// <summary>
/// 存放验证码值
/// </summary>
public string strValidate
{
get
{
if (Session["ValidateCode"] != null)
return Session["ValidateCode"].ToString();
else
return "";
}
set
{
Session["ValidateCode"] = value;
}
}
/// <summary>
/// 存放登入信息
/// </summary>
public string strUser
{
get
{
if (Session["strUser"] != null)
return Session["strUser"].ToString();
else
return "";
}
set
{
Session["strUser"] = value;
}
}
}
}
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.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Imaging;
using Security;
namespace Security
{
public partial class ValidateCode : System.Web.UI.Page
{
/// <summary>
/// 验证码长度
/// </summary>
private int codeLen = 6;
/// <summary>
/// 图片清晰度
/// </summary>
private int fineness = 85;
// 图片宽度
private int imgWidth = 80;
// 图片高度
private int imgHeight = 24;
// 字体家族名称
private string fontFamily = "Times New Roman";
// 字体大小
private int fontSize = 14;
// 字体样式
private int fontStyle = 0;
// 绘制起始坐标 X
private int posX = 0;
// 绘制起始坐标 Y
private int posY = 0;
protected void Page_Load(object sender, EventArgs e)
{
string validateCode = CreateValidateCode();//生成验证码
Bitmap bitmap = new Bitmap(imgWidth, imgHeight);// 生成BITMAP图像
DisturbBitmap(bitmap);// 图像背景
DrawValidateCode(bitmap, validateCode);// 绘制验证码图像
bitmap.Save(Response.OutputStream,ImageFormat.Gif);// 保存验证码图像,等待输出
}
/// <summary>
/// 生成验证码
/// </summary>
/// <returns></returns>
private string CreateValidateCode()
{
string validateCode = "";
string[] str =
{
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"
};
Random random = new Random();// 随机数对象
int n = 0;
for (int i = 0; i < codeLen; i++)
{
n = random.Next(str.Length);
validateCode += str[n];
}
this.strValidate = validateCode;// 保存验证码
return validateCode;
}
/// <summary>
/// 图像背景
/// </summary>
/// <param name="bitmap"></param>
private void DisturbBitmap(Bitmap bitmap)
{
Random random = new Random();// 通过随机数生成
for (int i = 0; i < bitmap.Width; i++)
{
for (int j = 0; j < bitmap.Height; j++)
{
if (random.Next(90) <= this.fineness)
bitmap.SetPixel(i, j, Color.White);
}
}
}
/// <summary>
/// 绘制验证码图像,bitmap 图板,validateCode 验证码值
/// </summary>
/// <param name="bitmap"></param>
/// <param name="validateCode"></param>
private void DrawValidateCode(Bitmap bitmap, string validateCode)
{
Graphics g = Graphics.FromImage(bitmap);// 获取绘制器对象
Font font = new Font(fontFamily, fontSize, FontStyle.Bold);// 设置绘制字体
g.DrawString(validateCode, font, Brushes.Black, posX, posY);// 绘制验证码图像
}
/// <summary>
/// 存放验证码值
/// </summary>
public string strValidate
{
get
{
if (Session["ValidateCode"] != null)
return Session["ValidateCode"].ToString();
else
return "";
}
set
{
Session["ValidateCode"] = value;
}
}
/// <summary>
/// 存放登入信息
/// </summary>
public string strUser
{
get
{
if (Session["strUser"] != null)
return Session["strUser"].ToString();
else
return "";
}
set
{
Session["strUser"] = value;
}
}
}
}