Wednesday, June 29, 2011

Protect your web site from bot?

By using recaptcha you can protect your web side from bot:

For using recaptcha in your web site you have need to download Recaptcha.dll from


After that add DLL as a reference as url website.

You can get your public key and private key from

http://www.google.com/recaptcha

here my page name was captcha.aspx: 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Captcha.aspx.cs" Inherits="Captcha" %>
<%@ Register TagPrefix="recaptcha" Namespace="Recaptcha" Assembly="Recaptcha" %>

<!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">
    <recaptcha:RecaptchaControl
    ID="recaptcha"
    runat="server"
    PublicKey="XXXXXXXXXX"
    PrivateKey="XXXXXXXXXX"
    />
    <div>
   
    <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />

   
    </div>
    </form>
</body>
</html>

Server side code for captcha.aspx is:

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 Recaptcha;

public partial class Captcha : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
             //True Conditon
        }
        else
        {
            //False Condition
        }
    }
}



For more information gone through following URL:

http://code.google.com/apis/recaptcha/docs/aspnet.html

Njoy buddy...:)

Sunday, June 26, 2011

ASP.NET tips and trick....?


Use StringBuilder for Complex String Manipulation

When a string is modified, the run time will create a new string and return it, leaving the original to be garbage collected. Most of the time this is a fast and simple way to do it, but when a string is being modified repeatedly it begins to be a burden on performance: all of those allocations eventually get expensive. Here's a simple example of a program that appends to a string 50,000 times, followed by one that uses a StringBuilder object to modify the string in place. The StringBuilder code is much faster, and if you run them it becomes immediately obvious.



namespace ConsoleApplication1.Feedback{
  using System;
  
  public class Feedback{
    public Feedback(){
      text = "You have ordered: \n";
    }

    public string text;

    public static int Main(string[] args) {
      Feedback test = new Feedback();
      String str = test.text;
      for(int i=0;i<50000;i++){
        str = str + "blue_toothbrush";
      }
      System.Console.Out.WriteLine("done");
      return 0;
    }
  }
}



namespace ConsoleApplication1.Feedback{
  using System;

  public class Feedback{
    public Feedback(){
      text = "You have ordered: \n";
    }

    public string text;

    public static int Main(string[] args) {
      Feedback test = new Feedback();
      System.Text.StringBuilder SB = 
        new System.Text.StringBuilder(test.text);
      for(int i=0;i<50000;i++){
        SB.Append("blue_toothbrush");
      }
      System.Console.Out.WriteLine("done");
      return 0;
    }
  }
 
 

Use Session State Only If You Need To

One extremely powerful feature of ASP.NET is its ability to store session state for users, such as a shopping cart on an e-commerce site or a browser history. Since this is on by default, you pay the cost in memory even if you don't use it. If you're not using Session State, turn it off and save yourself the overhead by adding <@% EnabledSessionState = false %> to your asp. This comes with several other options, which are explained at the ASP. NET Web site. For pages that only read session state, you can choose EnabledSessionState=readonly. This carries less overhead than full read/write session state, and is useful when you need only part of the functionality and don't want to pay for the write capabilities.

Use View State Only If You Need To

An example of View State might be a long form that users must fill out: if they click Back in their browser and then return, the form will remain filled. When this functionality isn't used, this state eats up memory and performance. Perhaps the largest performance drain here is that a round-trip signal must be sent across the network each time the page is loaded to update and verify the cache. Since it is on by default, you will need to specify that you do not want to use View State with <@% EnabledViewState = false %>. You should read more about View State on the the ASP. NET Web site to learn about some of the other options and settings to which you have access.

BackSpace problum with IE Toolbar...?

For Handling backspace IE toolbar Band object I need to include following thing:

Here tstxt_Name and tstxt_Password is my TextBox..



public IEToolbarEngine () 


         Assembly asm = Assembly.GetExecutingAssembly (); 
         string fullName = asm.GetModules () [0].FullyQualifiedName;
         toolbarFolder = Path.GetDirectoryName (fullName);
         dataFolder = DataFolder;


        try

         { 
                cacheFolder = Path.Combine (dataFolder, "Cache"); 
               Directory.CreateDirectory (cacheFolder); 
         }
        catch (Exception){}

         try

        {
           rssFoldr = Path.Combine (cacheFolder, "RSS");
           Directory.CreateDirectory (rssFolder);
        } 

      catch (Exception){}

       InitializeComponent ();

       toolStripLabel1.Size = new System.Drawing.Size(150, 21);
       tstxt_Name.GotFocus += new EventHandler(tstxt_Name_GotFocus);
      tstxt_Name.Focus();

      tstxt_Password.GotFocus += new EventHandler(tstxt_Password_GotFocus);
      tstxt_Password.Focus(); 

}


private void tstxt_Password_GotFocus(object sender, EventArgs e)


      this.OnGotFocus(e);
}

private void tstxt_Name_GotFocus(object sender, EventArgs e)

{
      this.OnGotFocus(e);
}

public override int TranslateAcceleratorIO(ref MSG msg)


       TranslateMessage(ref msg);
       DispatchMessage(ref msg);
       return 0;
}



Njoy buddy..:)