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

举报
小雨青年 发表于 2022/03/29 01:06:20 2022/03/29
【摘要】 点点细雨的原创博文!供各位在编程之路的同学们参考~ 本人博文允许转载,但请在文章显著位置注明转载出处以及原文链接,谢谢合作! 其实这个也不算是这个项目里用的啦,以前老师给推荐的,很好用哦! 页面的图像按钮 -> 链接地址为验证码的页面 ->...
点点细雨的原创博文!供各位在编程之路的同学们参考~
本人博文允许转载,但请在文章显著位置注明转载出处以及原文链接,谢谢合作

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

页面的图像按钮 -> 链接地址为验证码的页面 -> 验证码返回一个随机生成的图片到图像按钮并将图片的数字保存在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.HashPasswordForStoringIn ConfigFile(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);
            }
        }

文章来源: coderfix.blog.csdn.net,作者:小雨青年,版权归原作者所有,如需转载,请联系作者。

原文链接:coderfix.blog.csdn.net/article/details/16961983

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

举报
请填写举报理由
0/200