点点细雨的项目日记(五) 页…

简介: 点点细雨的原创博文!供各位在编程之路的同学们参考~本人博文允许转载,但请在文章显著位置注明转载出处以及原文链接,谢谢合作!其实这个也不算是这个项目里用的啦,以前老师给推荐的,很好用哦!页面的图像按钮 -> 链接地址为验证码的页面-> 验证码返回一个随机生成的图片到图像按钮并...
点点细雨的原创博文!供各位在编程之路的同学们参考~
本人博文允许转载,但请在文章显著位置注明转载出处以及原文链接,谢谢合作

其实这个也不算是这个项目里用的啦,以前老师给推荐的,很好用哦!

页面的图像按钮 -> 链接地址为验证码的页面 -> 验证码返回一个随机生成的图片到图像按钮并将图片的数字保存在SESSION里 ->页面通过文本框用户输入中的数值与 SESSION比较 -> 进行判断

源代码:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing;

namespace AnboSchoolServeSystem.Admin
{
    public partial class VerityCode : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            this.CreateImage(this.GenerateNumber(4));
        }
        private void CreateImage(string checkCode)
        {
            int iwidth = (int)(checkCode.Length * 15);
            System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 20);
            Graphics g = Graphics.FromImage(image);
            g.Clear(Color.White);
            Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };
            string[] font = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体" };
            Random rand = new Random();
            for (int i = 0; i < 50; i++)
            {
                int x = rand.Next(image.Width);
                int y = rand.Next(image.Height);
                g.DrawRectangle(new Pen(Color.LightGray, 0), x, y, 1, 1);
            }
            for (int i = 0; i < checkCode.Length; i++)
            {
                int cindex = rand.Next(7);
                int findex = rand.Next(5);
                Font f = new System.Drawing.Font(font[findex], 12, System.Drawing.FontStyle.Bold);
                Brush b = new System.Drawing.SolidBrush(c[cindex]);
                int ii = 4;
                if ((i + 1) % 2 == 0)
                {
                    ii = 2;
                }
                g.DrawString(checkCode.Substring(i, 1), f, b, 3 + (i * 12), ii);
            }
            g.DrawRectangle(new Pen(Color.Black, 0), 0, 0, image.Width - 1, image.Height - 1);

            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            Response.ClearContent();
            Response.ContentType = "Image/Jpeg";
            Response.BinaryWrite(ms.ToArray());
            g.Dispose();
            image.Dispose();
        }
        public string GenerateNumber(int codeLength)
        {//,W,E,R,T,Y,U,I,P,L,K,J,H,G,F,S,A,Z,X,C,V,B,N,M
            string Vchar = "1,2,3,4,5,6,7,8,9";
            string[] VcArray = Vchar.Split(',');
            string VNum = "";
            Random random = new Random();
            for (int i = 0; i <= codeLength; i++)
            {
                int iNum = 0;
                while ((iNum = Convert.ToInt32(VcArray.Length * random.NextDouble())) == VcArray.Length)
                {
                    iNum = Convert.ToInt32(VcArray.Length * random.NextDouble());
                }
                VNum += VcArray[iNum];
            }
            Session["VerifyCode"] = VNum.ToString().Trim();
            return VNum;
        }
    }
} 

------------------------------------------------------------------------------
页面源代码片段:

                          <tr>
                            <td height="35" class="style1" ><span class="login_txt">验证码:</span></td>
                            <td height="35" colspan="2" class="top_hui_text">   <asp:TextBox ID="txtVerityCode" 
                                    runat="server" ontextchanged="txtVerityCode_TextChanged"></asp:TextBox>
                                <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/Admin/VerityCode.aspx" ToolTip="点击切换" />
                               
                              </td>

cs文件中的事件:

protected void btnLogin_Click(object sender, EventArgs e)
        {
            string username=txtName.Text.ToString().Trim();
            string userpwd=txtPwd.Text.ToString().Trim();
             
            string code = Session["VerifyCode"].ToString();
            if (txtVerityCode.Text.Trim() == "")
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "lb", "<script language='javascript'>alert('验证码输入不能为空!');</script>", false);
            }
            else if (txtVerityCode.Text.Trim() != code)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "lb", "<script language='javascript'>alert('验证码输入错误,请重新输入!');</script>", false);
            }
            else
            {
                
                users  users= usersBll.GetUsersByNameAndPwd(username, FormsAuthentication.HashPasswordForStoringInConfigFile(userpwd, "SHA1"));
                if (users != null)
                {
                    //Session.Add("CurrentUser", admin);
                    //if (LogsManager.AddLogs(admin.UserID))
                    //{
                        Response.Redirect("index.aspx");
                        Session["users_id"] = users.Users_id;
                        if (users.Users_power == true)
                        {
                            Session["superadmin"] = true;
                        }
                        
                    //}
                    //else
                    //{
                    //    Session.Abandon();
                    //    Page.RegisterStartupScript("err2", "<script>alert('日志写入失败,请重试!');</script>");
                    //}
                }
                else
                {
                    Page.RegisterStartupScript("err3", "<script>alert('密码或者用户名不正确,请重新输入!');document.getElementByIdx_x('txtUserName').select();document.getElementByIdx_x('txtUserName').focus();</script>");
                    return;
                }
            }
        }

        protected void txtVerityCode_TextChanged(object sender, EventArgs e)
        {
            string code = Session["VerifyCode"].ToString();
            if (txtVerityCode.Text.Trim() != code)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "lb", "<script language='javascript'>alert('验证码输入错误,请重新输入!');</script>", false);
            }
        }
目录
相关文章
|
3月前
PPT修改模板作者备注
PPT修改模板作者备注
16 0
仿写一个登录页(布局篇)|青训营笔记
在使用 APP 派对岛 的时候,发现它的页面很好看,同时其首要的登录方法也是跳转去采用抖音来登录的。符合当前的业务需求,所以决定仿写该页面作为大作业的登录界面。
仿写一个登录页(布局篇)|青训营笔记
|
Python
自制操作系统日记(7):字符串显示
上篇中我们在屏幕上画出了界面的大致轮廓,系统有了点模样,本篇继续跟着书籍,让程序中的字符串显示在屏幕上
|
存储 前端开发 JavaScript
#yyds干货盘点# 前端歌谣的刷题之路-第一百零八题-切换tab栏目
#yyds干货盘点# 前端歌谣的刷题之路-第一百零八题-切换tab栏目
78 0
#yyds干货盘点# 前端歌谣的刷题之路-第一百零八题-切换tab栏目
|
XML 前端开发 数据格式
项目第九天内容介绍 | 学习笔记
快速学习 项目第九天内容介绍
项目第九天内容介绍 | 学习笔记
|
SQL 开发者 微服务
项目第八天内容介绍 | 学习笔记
快速学习 项目第八天内容介绍
项目第八天内容介绍 | 学习笔记
|
运维 jenkins 持续交付
项目第十九天内容的介绍 | 学习笔记
快速学习 项目第十九天内容的介绍
|
分布式数据库 开发者 Hbase
获取微博内容 &amp;过器介绍|学习笔记
快速学习 获取微博内容 &amp;过器介绍
实战项目:通讯录&amp;nbsp;UI—第十一天
 1、推出视图的两种方式:  1.通过导航控制器push到下一个界面,使用pop返回到上一个界面  2.通过模态的形式推出视图,不需要依赖于导航控制器,通过使用present到下一个界面,通过dismiss返回到上一个界面 如何选择使用哪种视图的推出方法?  秘诀:看视图之间的依赖关系  1.
900 0
寻找春天&amp;nbsp;九宫格日记-2014.04.26
写九宫格日记总会在听到某一首歌的时候泪流满面;总会在看到某个似曾相识的背影的时候惆怅莫名,总会在嗅到某种香味的时候默默发呆,总会在经过某个地方的时候频频回首。生命有限,不要把它浪费在重复别人的生活上。
683 0