Hi guys,
For using captcha
validation first we create a captchaimage.aspx page and call the page within
the registration.aspx img tab imageurl =”captchaimage.aspx”. This <asp:image>
tag used within registration page for
display captcha image. For creae a image.aspx file we need SRVTextToImage.dll
file . So we can download it first .
Captchaimage.aspx.cs
file
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SRVTextToImage;
using System.Drawing.Imaging;
using System.Drawing;
public partial class captchaimage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//captchaimage
CI = new captchaimage();
CaptchaRandomImage CI = new CaptchaRandomImage();
//
GetRandomString Function return random text of your provided characters size
string captchaText = CI.GetRandomString(5);
//GenearteImage function return image of the provided text of
provided size
//CI.GenerateImage(captchaText,
200, 50);
//there
is a overload function available for set color of the image
Session["CaptchaText"] = captchaText;
CI.GenerateImage(captchaText, 200, 50, Color.DarkGray, Color.White);
this.Response.Clear();
this.Response.ContentType = "image/jpeg";
CI.Image.Save(this.Response.OutputStream, ImageFormat.Jpeg);
CI.Dispose();
}
}
Now we create a registration page for use it.
Registration.aspx
<table class="auto-style1">
<tr>
<td> </td>
<td>
<table class="auto-style1">
<tr>
<td class="auto-style2">
<asp:Label ID="Label1" runat="server" Font-Bold="True" ForeColor="#000099" Text="UserId"></asp:Label>
</td>
<td class="auto-style3">
<asp:TextBox ID="txtUserId" runat="server" Width="271px"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtUserId" ErrorMessage="Enter a valid Userid" Font-Bold="True" ForeColor="#FF3300">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="auto-style2">
<asp:Label ID="Label2" runat="server" Font-Bold="True" ForeColor="#0033CC" Text="UserName"></asp:Label>
</td>
<td class="auto-style3">
<asp:TextBox ID="txtFirstName" runat="server" Width="269px"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtFirstName" ErrorMessage="Enter a valid Firstname" Font-Bold="True" ForeColor="Red">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="auto-style2">
<asp:Label ID="Label8" runat="server" Font-Bold="True" ForeColor="#3333FF" Text="PassWord"></asp:Label>
</td>
<td class="auto-style3">
<asp:TextBox ID="txtPassWord" runat="server" TextMode="Password" Width="271px"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ControlToValidate="txtPassWord" ErrorMessage="Enter a valid password" Font-Bold="True" ForeColor="Red">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="auto-style2">
<asp:Label ID="Label3" runat="server" Font-Bold="True" ForeColor="Blue" Text="LastName"></asp:Label>
</td>
<td class="auto-style3">
<asp:TextBox ID="txtLastName" runat="server" Width="273px"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtLastName" ErrorMessage="Enter a valid lastname" Font-Bold="True" ForeColor="Red">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="auto-style11">
<asp:Label ID="Label4" runat="server" Font-Bold="True" ForeColor="Blue" Text="Email"></asp:Label>
</td>
<td class="auto-style12">
<asp:TextBox ID="txtEmail" runat="server" Width="272px"></asp:TextBox>
</td>
<td class="auto-style13">
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtEmail" ErrorMessage="Enter a valid email id" Font-Bold="True" ForeColor="Red" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">*</asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="txtEmail" ErrorMessage="Enter a email " Font-Bold="True" ForeColor="Red">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="auto-style14">
<asp:Label ID="Label5" runat="server" Font-Bold="True" ForeColor="Blue" Text="Gender"></asp:Label>
</td>
<td class="auto-style15">
<asp:DropDownList ID="ddlGender" runat="server" Height="30px" Width="176px">
<asp:ListItem>--Select--</asp:ListItem>
<asp:ListItem>Male</asp:ListItem>
<asp:ListItem>Female</asp:ListItem>
</asp:DropDownList>
</td>
<td class="auto-style16">
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="ddlGender" ErrorMessage="Enter a vaild
gender" Font-Bold="True" ForeColor="Red">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="auto-style7">
<asp:Label ID="Label6" runat="server" Font-Bold="True" ForeColor="Blue" Text="Capt"></asp:Label>
</td>
<td class="auto-style8">
<asp:Image ID="Imagecaptcha" ImageUrl="~/captchaimage.aspx" runat="server" />
</td>
</tr>
<tr>
<td class="auto-style2">
<asp:Label ID="Label7" runat="server" Font-Bold="True" ForeColor="#3366FF" Text="Captcha Text"></asp:Label>
</td>
<td class="auto-style3">
<asp:TextBox ID="txtCaptcha" runat="server" Width="269px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style2"> </td>
<td class="auto-style3">
<asp:ImageButton ID="btnRegister" runat="server" Height="32px" ImageUrl="~/Images/register.jpg" Width="120px" OnClick="btnRegister_Click" />
<asp:Label ID="lblMsg" runat="server"></asp:Label>
</td>
</tr>
</table>
</td>
<td> </td>
</tr>
</table>
Registration.aspx.cs
file
public partial class UserRegister : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["123"].ConnectionString);
protected void btnRegister_Click(object sender, ImageClickEventArgs e)
{
// Here
I will validate Captcha
bool isCaptchaValid = false;
if (Page.IsPostBack && (Session["CaptchaText"] != null && txtCaptcha.Text
== Session["CaptchaText"].ToString()))
{
isCaptchaValid = true;
}
if (isCaptchaValid)
{
//if
(Page.IsValid && (Session["CaptchaText"] != null &&
txtCaptchaText.Text == Session["CaptchaText"].ToString()))
//if (Page.IsValid
&& (Session["CaptchaText"] != null && txtCaptcha.Text
= Session["CaptchaText"].ToString()))
try
{
SqlCommand cmd = new SqlCommand("sanregister", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@UserId", SqlDbType.Int)).Value =
txtUserId.Text;
cmd.Parameters.Add(new SqlParameter("@UserName", SqlDbType.VarChar, 50)).Value
= txtFirstName.Text;
cmd.Parameters.Add(new SqlParameter("@PassWord", SqlDbType.VarChar, 50)).Value
= txtPassWord.Text;
cmd.Parameters.Add(new SqlParameter("@LastName", SqlDbType.VarChar, 50)).Value
= txtLastName.Text;
cmd.Parameters.Add(new SqlParameter("@Email", SqlDbType.VarChar, 50)).Value
= txtEmail.Text;
cmd.Parameters.Add(new SqlParameter("@Gender", SqlDbType.VarChar, 50)).Value
= ddlGender.SelectedValue.ToString();
cmd.Parameters.Add(new SqlParameter("@Capt", SqlDbType.VarChar, 50)).Value
= txtCaptcha.Text;
//string Ins;
//Ins = " insert into mycap values('" +
txtEmailID.Text + "','" + txtFeedback.Text + "','" +
txtCaptchaText.Text + "')";
//cmd = new SqlCommand(Ins, con);
con.Open();
Session["user"] =
txtFirstName.Text;
cmd.ExecuteNonQuery();
con.Close();
//lblMessage.Text = "Captcha Validation
Success";
//lblMsg.Text = "Successfully insert data";
Response.Redirect("Mainaccess.aspx");
//
lblMsg.ForeColor = Color.Green;
}
catch (Exception er)
{
lblMsg.Text = er.Message;
}
}
else
{
lblMsg.Text = "Captcha Validation Failed";
lblMsg.ForeColor = Color.Red;
}
}
No comments:
Post a Comment