加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ASP.NET 做验证码

发布时间:2020-12-17 07:59:19 所属栏目:百科 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 显示验证码的界面:前台:td class="style4" 验证码:/td td class="style2" asp:TextBox ID="TextBox3" runat="server" Height="21px"/asp:TextBox a

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

显示验证码的界面:
前台:
<td class="style4">
                    验证码:</td>
                <td class="style2">
                    <asp:TextBox ID="TextBox3" runat="server" Height="21px"></asp:TextBox>
                    <asp:Image ID="Image1" runat="server" ImageUrl="~/Default2.aspx"/>
                    &nbsp;<asp:HyperLink ID="HyperLink1" runat="server"
                        NavigateUrl="~/Default.aspx">看不清换张图</asp:HyperLink>
                </td>
后台:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender,EventArgs e)
    {
 
    }
    protected void Button1_Click(object sender,EventArgs e)
    {
        string str = ConfigurationManager.ConnectionStrings["sqlstr"].ConnectionString;
        using (SqlConnection sqlCnn=new SqlConnection(str))
        {
            using (SqlCommand sqlCmm=sqlCnn.CreateCommand())
            {
                sqlCmm.CommandText = "select * from Login";
                sqlCnn.Open();
                SqlDataReader reader = sqlCmm.ExecuteReader();
                bool bl = false;
                if (reader!=null)
                {
                    while (reader.Read())
                    {
                        if (this.TextBox1.Text == reader["name"].ToString() && this.TextBox2.Text == reader["password"].ToString() && this.TextBox3.Text == Session["code"].ToString())
                        {
                            bl = true;
                        }
                    }
                    if (bl==true)
                    {
                        ClientScript.RegisterClientScriptBlock(GetType(),"提示","<script>alert('登陆成功!')</script>");
                    }
                    else
                    {
                        ClientScript.RegisterClientScriptBlock(GetType(),"<script>alert('登陆失败!')</script>");
                    }
                }
            }
        }
    }
}
做验证码的界面:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.IO;
using System.Text;
public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender,EventArgs e)
    {
        System.Drawing.Image img = new Bitmap(100,20);
        Graphics g = Graphics.FromImage(img);
        this.DealImage(img,10);
        g.DrawLine(Pens.Yellow,100,20);
        string code = this.GenerateCode();
 
        Font font = new Font("宋体",20,FontStyle.Bold | FontStyle.Italic | FontStyle.Strikeout | FontStyle.Underline);
        Session["code"] = code;                                 //保存到Session中
        g.DrawString(code,font,Brushes.YellowGreen,0);
        this.Response.Clear();//http://www.baoluowanxiang.com
        MemoryStream ms = new MemoryStream();
        img.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
        this.Response.BinaryWrite(ms.ToArray());
        this.Response.Flush();
        this.Response.End();
 
    }
    private void DealImage(System.Drawing.Image img,int nums)
    {
        Bitmap b = img as Bitmap;
        Random ran = new Random();
        for (int i = 0; i < nums; i++)
        {
            b.SetPixel(ran.Next(0,img.Width),ran.Next(0,img.Height),Color.White);
        }
    }
    private string GenerateCode()   //产生随机数
    {
        Random ran = new Random(DateTime.Now.Millisecond);
        StringBuilder sb = new StringBuilder(6);
        for (int i  = 0; i  <6; i ++)
        {
            sb.Append(ran.Next(0,9));
        }
        return sb.ToString();
    }
}
 

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读