Friday, 7 February 2014

count No of User Visited My web Site

Global asax:-

<% Application Language="C#"%>
<Script Runat="server" >
Public void Application_Start(Object Sender EventArgs e)
{
Application["No of user visited  My web Site"]=0;
}

public void Session Start(Object Sender Event Args e)
{
Application.Lock();
Application["No of User visited my web Site]=(int)Application["No of user Visited My web Site"] +1;
Application.Unlock();
}
</Script>

Default Page
<br>
No of user Visited web site
<tr>
<td>
<asp:Label Id="lblcount" runat="server">
</td>
</tr>
Page Load

lblCount.Text=Application["No of  user Visited my web Site"].To String());







Monday, 3 February 2014

CAPTCHA

CAPTCHA: A best technique to restrict automatic form submission when developing a web page is to add some kind of verification.

HANDLER
<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;
using System.Web.SessionState;
using System.Drawing;
using System.Drawing.Imaging;

public class Handler : IHttpHandler,IRequiresSessionState
{
    public void ProcessRequest(HttpContext context)
    {
        using (Bitmap b = new Bitmap(250, 50))
        {
            Font f = new Font("Arial", 10F);
            Graphics g = Graphics.FromImage(b);
            SolidBrush whiteBrush = new SolidBrush(Color.Blue);
            SolidBrush blackBrush = new SolidBrush(Color.White);
            RectangleF canvas = new RectangleF(0, 0, 250, 50);
            g.FillRectangle(whiteBrush, canvas);           
            context.Session["Captcha"] = GetRandomString();
            g.DrawString(context.Session["Captcha"].ToString(), f, blackBrush, canvas);
            context.Response.ContentType = "image/gif";
            b.Save(context.Response.OutputStream, ImageFormat.Gif);
        }
    }
   
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
   
    private string GetRandomString()
    {
        string []arrStr = "A,B,C,D,1,2,3,4,5,6,7,8,9,0".Split(",".ToCharArray());
        string strDraw = string.Empty;
        Random r = new Random();        
         for(int i = 0; i < 5 ; i++)
         {
              strDraw += arrStr[r.Next(0,arrStr.Length-1)];
         }       
        return strDraw;
    }
}
 

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">

<div>

<img src="Handler.ashx" />

<br />

Please type above text here. To identify your self as a human

<br />

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />

<br />

<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />

<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></div>

</form>

</body>
</html>

CODE BEHIND
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Session["Captcha"].ToString() == TextBox1.Text)
        {
            Label1.Text = " Code Matched!";
        }
        else
        {
            Label1.Text = "Please try again";
            TextBox1.Text = "";
        }
    }
}