Pages

Saturday, September 18, 2010

What is OOP?

OOP is a design philosophy.
It stands for Object Oriented Programming.
Object-Oriented Programming (OOP) uses a different set of programming languages other than old procedural programming languages (C, Pascal, etc.).
Everything in OOP is grouped as self sustainable "objects". Hence, you gain re-usability by means of four main object-oriented programming concepts (Encapsulation, Inheritance, Polymorphism, Abstraction).
In order to clearly understand the object orientation, let’s take your “hand” as an example.

The “hand” is a class. Your body has two objects of type hand, named left hand and right hand. Their main functions are controlled/ managed by a set of electrical signals sent through your shoulders (through an interface).
So the shoulder is an interface which your body uses to interact with your hands. The hand is a well architected class. The hand is being re-used to create the left hand and the right hand by slightly changing the properties of it. (More to follow in next post..........)

Wednesday, July 21, 2010

Generating and enforcing that any link requested is converted to lowercase Using ASP.NET(C#)



Generating and enforcing that any
link requested is converted to lowercase Using ASP.NET(C#)



Asp.Net Version: All Asp.net Version
supports this method.



Some search
engines interpret URLs with different case sensitivity differently for e.g. “http://www.example.com/Home.aspx”
and “http://www.example.com/home.aspx” as two different locations.



So now the
problem arises how we can enforce that all my requested URLs get rendered as
lowercase, and how do I force a request to a URL with an uppercase letter to
redirect to the lowercase equivalent so that Search engine stops treating URLs
having different cases differently.



To ensure
that any request to the site is enforced to be lowercased, and perform a 301
redirect if not I have created an HttpModule, and named it EnforceLowecaseRequestHttpModule.



To integrate
this we can either register it in the web.config file or we could directly put
this into the global.asax file.



I will
suggest creating an HttpModule and integrate it in web.config file to make it
easily reusable on future projects.



Let’s follow
step by step procedure to create HttpModule and get register in Web.Config
file.



Step 1.
First create a file (EnforceLowercaseRequestHttpModule.cs) in App_code folder
in you applications and paste the following code:



using System;



using System.Data;



using System.Configuration;



using System.Web;



using System.Web.Security;



using System.Web.UI;



using System.Text;



using System.Text.RegularExpressions;



public
class EnforceLowercaseRequestHttpModule : IHttpModule



{



public void Dispose()



{



}



//Call
Init functionto register function



public
void Init(HttpApplication context)



{



context.BeginRequest +=
context_BeginRequest;



}





//Declare
function where requested URL is checked for any UpperCase and if //found
convert it to LowerCase





private
void context_BeginRequest(object sender, EventArgs e)



{



HttpApplication application =
(HttpApplication)sender;





//
If upper case letters are found in the URL then convert and //redirect to lower case URL





string
requestedUrl = ((application.Context.Request.Url.Scheme + "://" +
application.Context.Request.Url.Authority +
application.Context.Request.Url.AbsolutePath));





if
(Regex.IsMatch(requestedUrl, @"[A-Z]") == true)



{



string
lowercaseUrl = requestedUrl.ToLower();



lowercaseUrl
+= HttpContext.Current.Request.Url.Query;





application.Context.Response.Clear();



application.Context.Response.Status
= "301 Moved Permanently";



application.Context.Response.AddHeader("Location",
lowercaseUrl);



application.Context.Response.End();



}



}



}



Step 2. Next
step is to register this HttpModule in the project and to do this we will
follow simple steps:



Open
Web.config file and paste the following code in “<httpModules>” tag as
follows.



To register the module for IIS 6.0
and IIS 7.0 running in Classic mode



<configuration>




<system.web>




<httpModules>



<add name="EnforceLowercaseRequestHttpModule" type="EnforceLowercaseRequestHttpModule"/>




</httpModules>




</system.web>



</configuration>



Registering the HTTP Module in IIS
7.0 Integrated Mode



<configuration>




<system.webServer>




<modules>



<add name="EnforceLowercaseRequestHttpModule" type="EnforceLowercaseRequestHttpModule"/>




</modules>




</system.webServer>



</configuration>



That’s all
and now this can easily handle URLs having UpperCase characters to lower case.



Happy
coding………………………………….



Tuesday, June 29, 2010

Friday, March 26, 2010

Guard Against Denial-of-Service Threats in ASP.NET

An indirect way that a malicious user can compromise your application is by making it unavailable.


The malicious user can keep the application too busy to service other users, or if can simply cause the application to crash. Follow these guidelines:


  • Use error handling (for example, try-catch). Include a finally block in which you release resources in case of failure.
  • Configure IIS to use process throttling, which prevents an application from using up a disproportionate amount of CPU time.
  • Test size limits of user input before using or storing it.
  • Put size safeguards on database queries. For example, before you display query results in an ASP.NET Web page, be sure that there are not an unreasonable number of records.
  • Put a size limit on file uploads, if those are part of your application. You can set a limit in the Web.config file using syntax such as the following, where the maxRequestLength value is in kilobytes:




Thursday, March 25, 2010

Post data to another host specially to payment gateway (Authorize.net, blue fin payment gateway)

//Create an object of hash table
Hashtable post_values = new Hashtable();

//Add values in the hash table
post_values.Add("tran_type", "A");
post_values.Add("account_id",”AccountId”);

post_values.Add("pay_type", "C");

post_values.Add("card_cvv2",”cardCVV number”);

post_values.Add("card_number", “Card Number”);

post_values.Add("card_expire", “Card Expiry Date”);

post_values.Add("bill_name1,”Customer First Name”);

post_values.Add("bill_name2",”Customer Last Name”);

post_values.Add("bill_street, ”BillingAddress1”);

post_values.Add("bill_state", “bill_state”);



post_values.Add("bill_zip",” BillingZipCode”);



post_values.Add("bill_country", “bill_country”);

post_values.Add("amount,"totalorderamount");



// create an HttpWebRequest object to communicate with payment gateway site

HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(“PaymentGatewayURL”);



//Assign Methor by which request will be send

objRequest.Method = "POST";

objRequest.ContentLength = post_string.Length;

objRequest.ContentType = "application/x-www-form-urlencoded";

// post data is sent as a stream

StreamWriter myWriter = null;

myWriter = new StreamWriter(objRequest.GetRequestStream());

myWriter.Write(post_string);

myWriter.Close();



// returned values are returned as a stream, then read into a string

String post_response;

HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();

//Read response to the end response value will be stored in variable //post_response



using (StreamReader responseStream = new StreamReader(objResponse.GetResponseStream()))

{

post_response = responseStream.ReadToEnd();

responseStream.Close();

}