如何在 asp.net 与 ASp:Login 一起使用的登录表单中添加验证码

本文关键字:登录 表单 验证 添加 一起 asp net Login ASp | 更新日期: 2023-09-27 18:33:02

我正在使用asp:login对象进行登录,并且还使用成员资格安全性
我对此感到非常困惑,因为当我想显示有关不正确登录和不正确验证码的消息时,它对我不起作用。
请告诉我一个简单的解决方案,我可以为每个错误设置消息。

谢谢

如何在 asp.net 与 ASp:Login 一起使用的登录表单中添加验证码

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="CatchpaRegistrationpage.aspx.cs" Inherits="Employee_CatchpaRegistrationpage" %>
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:ScriptManager ID="SM1" runat="server">
                </asp:ScriptManager>
                <table style="border: solid 1px black; padding: 20px; position: relative; top: 50px;"
                    align="center">
                    <tr>
                        <td>EmailID :
                        </td>
                        <td>
                            <asp:TextBox ID="txtEmailID" runat="server" Width="200px"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>Password :
                        </td>
                        <td>
                            <asp:TextBox ID="txtPassword" runat="server" TextMode="Password" Width="200px"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>Confirm Password :
                        </td>
                        <td>
                            <asp:TextBox ID="txtConfirmPassword" runat="server" TextMode="Password" Width="200px"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>Enter Below Code :
                        </td>
                        <td>
                            <asp:TextBox ID="txtCaptcha" runat="server" Width="200px"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td></td>
                        <td valign="middle">
                            <asp:UpdatePanel ID="UP1" runat="server">
                                <ContentTemplate>
                                    <table>
                                        <tr>
                                            <td style="height: 50px; width: 100px;">
                                                <asp:Image ID="imgCaptcha" runat="server" />
                                            </td>
                                            <td valign="middle">
                                                <asp:Button ID="btnRefresh" runat="server" Text="Refresh" OnClick="btnRefresh_Click" />
                                            </td>
                                        </tr>
                                    </table>
                                </ContentTemplate>
                            </asp:UpdatePanel>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2" align="center">
                            <asp:Button ID="btnRegiser" runat="server" Text="Register" OnClick="btnRegister_Click" />
                        </td>
                    </tr>
                </table>
            </div>
        </form>
    </body>
    </html>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

public partial class Employee_CatchpaRegistrationpage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FillCapctha();
        }
    }
    void FillCapctha()
    {
        try
        {
            Random random = new Random();
            string combination = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
            StringBuilder captcha = new StringBuilder();
            for (int i = 0; i < 6; i++)
            {
                captcha.Append(combination[random.Next(combination.Length)]);
                Session["captcha"] = captcha.ToString();
                imgCaptcha.ImageUrl = "CatchpaRegistrationpage.aspx?" + DateTime.Now.Ticks.ToString();
            }
        }
        catch
        {
            throw;
        }
    }
    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        FillCapctha();
    }
    protected void btnRegister_Click(object sender, EventArgs e)
    {
        if (Session["captcha"].ToString() != txtCaptcha.Text)
        {
            Response.Write("Invalid Captcha Code");
        }
        else
        {
            Response.Write("Valid Captcha Code");
        }
        FillCapctha();
    }
}