• asp.net下登陆验证码实现


    登陆页面-验证码的实现:

    view页面:

    <asp:Content ID="loginContent" ContentPlaceHolderID="MainContent" runat="server">
        <h2>Log On</h2>
        <p>
            Please enter your username and password. <%= Html.ActionLink("Register", "Register") %> if you don't have an account.
        </p>

        <% using (Html.BeginForm()) { %>
            <%= Html.ValidationSummary(true, "Login was unsuccessful. Please correct the errors and try again.") %>
            <div>
                <fieldset>
                    <legend>Account Information</legend>
                   
                    <div class="editor-label">
                        <%= Html.LabelFor(m => m.UserName) %>
                    </div>
                    <div class="editor-field">
                        <%= Html.TextBoxFor(m => m.UserName) %>
                        <%= Html.ValidationMessageFor(m => m.UserName) %>
                    </div>
                   
                    <div class="editor-label">
                        <%= Html.LabelFor(m => m.Password) %>
                    </div>
                    <div class="editor-field">
                        <%= Html.PasswordFor(m => m.Password) %>
                        <%= Html.ValidationMessageFor(m => m.Password) %>
                    </div>
                   
                    <div class="editor-label">
                        <%= Html.CheckBoxFor(m => m.RememberMe) %>
                        <%= Html.LabelFor(m => m.RememberMe) %>
                    </div>
                    <div>
                    <table>
                    <tr>
                    <td>验证码</td>
                    <td><img src="CheckImage.aspx" width="120" height="30" style="cursor:hand"
                         onclick="this.src='CheckImage.aspx?id='+Math.random()*10000" alt="点击刷新验证码" /></td>
                    </tr>
                    </table>
                    </div>
                    <p>
                        <input type="submit" value="Log On" />
                    </p>
                </fieldset>
            </div>
        <% } %>

    后台:

    protected void Page_load(object sender,EventArgs e)

    {

    //验证码中可能出现的字符集合

    string checkCodeString = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
                int length = checkCodeString.Length;

                Font font = new Font("宋体",24,FontStyle.Bold);//设置宋体绘制验证字符,并加粗
                Brush brush = null;//绘制验证码的brush对象
                Color brushColor = new Color();//绘制验证码的颜色

                string checkCode = string.Empty;//显示给用户的验证码字符串
                string code = string.Empty;//要绘制的验证码字符

                Bitmap image = new Bitmap(80,40);
                Graphics g = Graphics.FromImage(image);//实例化图表
                g.Clear(Color.White);

                Random random = new Random();//生成随机数
                for (int i = 0; i < 4; i++)
                {
                    int current = random.Next(DateTime.Now.Millisecond) % length;//采用模运算,保证current不会超过验证码集合的长度
                    code = checkCodeString.Substring(current,1);//从验证码中随机截取一个字符来绘制
                    checkCode = checkCode + code;
                    brushColor = Color.FromArgb(random.Next(255),random.Next(255),random.Next(255));//随机生成绘制验证码字符的颜色
                    brush = new SolidBrush(brushColor);
                    g.DrawString(code,font,brush,i*15+2,2);//绘制刚刚得到的字符串,(i*15+2)用来设定X轴,以区分字符间距
                }
                Response.Clear();//清除无用信息
                Response.ContentType = "image/pjpeg";
                image.Save(Response.OutputStream,ImageFormat.Jpeg);//将图像保存到response输出流

                Session["CheckCode"] = checkCode;//保存,以便于同用户输入对比
                image.Dispose();//释放资源
                Response.End();//结束输出

    }

  • 相关阅读:
    Jackson 框架,轻易转换JSON
    Java Thread 多线程 操作线程
    Oracle笔记 四、增删改、事务
    Oracle笔记 六、PL/SQL简单语句块、变量定义
    Java 的JSON、XML转换方法——目录索引
    SpringMVC 中整合JSON、XML视图一
    FreeMarker整合Spring 3
    Java Thread 多线程 介绍
    Jaxb2 转换XML文档
    Java Thread 多线程同步、锁、通信
  • 原文地址:https://www.cnblogs.com/mountain2011/p/2591608.html
Copyright © 2020-2023  润新知