C#实现登录验证码

本文介绍如何在C#中实现登录验证码功能,通过创建一个ValidateCode.ashx文件来生成验证码,并在登录界面中展示。登录时,检查用户输入的验证码是否与服务器生成的验证码一致,确保登录安全性。
摘要由CSDN通过智能技术生成

 最近在开发中遇到要实现登录验证码的功能,在网上借鉴了一下并加以扩展,供大家分享.

代码如下:

///首先新建一个ValidateCode.ashx文件.

using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Drawing;

namespace WebApplication3
{
    /// <summary>
    /// Summary description for $codebehindclassname$
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class ValidateCode : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.Cache.SetCacheability(HttpCacheability.NoCache);///特别注意,如不加,单击验证图片'看不清换一张',无效果.
            this.CreateCheckCodeImage(GenerateCheckCode(context),context);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        private string GenerateCheckCode(HttpContext context)
        {
            int number;
            char code;
            string checkCode = String.Empty;

            System.Random random = new Random();

            for (int i = 0; i < 6; i++)
            {
                number = random.Next();

                if (number % 2 == 0)
  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值