Friday, August 14, 2009

New version of Google Search Engine

In case anyone missed this news, Google are releasing a new version of their search engine to the public http://www2.sandbox.google.com/.  I did not see any real difference with the existing one but maybe you will.

Apparently: “For the last several months, a large team of Googlers has been working on a secret project: a next-generation architecture for Google’s web search. It’s the first step in a process that will let us push the envelope on size, indexing speed, accuracy, comprehensiveness and other dimensions. The new infrastructure sits “under the hood” of Google’s search engine, which means that most users won’t notice a difference in search results. But web developers and power searchers might notice a few differences, so we’re opening up a web developer preview to collect feedback.”

http://googlewebmastercentral.blogspot.com/2009/08/help-test-some-next-generation.html

Tuesday, August 11, 2009

Encrypting your web.config file

Ever see a connection string in a configuration file with the Username and Password available to the world?  I know I have, far to many time to be good.  But; did you know that you can quite easily Encrypt and Decrypt this on-the-fly using built in .Net functionality?!?!  Well you can and it’s very easy to do too.

Setting up your project

First create a simple ASP.NET web application in Visual Studio 2008.

  • Edit the current web.config file and add a valid connection string:

<connectionStrings>
  <add name="MyAppConnection" connectionString="Server=dubwsdev001;Database=DevDatabase;Integrated Security=false;User Id=xxxxx;PWD=xxxxxxxxx;" />
</connectionStrings>

  • Edit the default.aspx file

Add two labels and two button to your form or just copy/paste the following code:

<form id="form1" runat="server">
<div>  
    <asp:Label ID="lblStatus" runat="server"></asp:Label>
    <br />
    <asp:Label ID="lblConnection" runat="server"></asp:Label>
    <br />

    <asp:Button ID="btnEncrypt" runat="server" onclick="EncryptConfig" Text="Encrypt" />
        &nbsp;&nbsp;
    <asp:Button ID="btnDecrypt" runat="server" Text="Decrypt" onclick="DecryptConfig" />

    <br />

</div>
</form>

It should look something like the following:

image

  • Edit the code behind “default.aspx.cs” file to populate the lblConnection text label with the connection string we have in the web.config file.  Do this by adding the following code:

using System.Web.Configuration;

protected void Page_Load(object sender, EventArgs e)
{
     string strConnection= WebConfigurationManager.ConnectionStrings["MyAppConnection"].ConnectionString;
     lblConnection.Text = strConnection;
}

protected void EncryptConfig(object sender, EventArgs e)
{

}

protected void DecryptConfig(object sender, EventArgs e)
{

}

Starting the application should result in the following:

image

Encrypt / Decrypt on-the-fly

To do this we need 3 private methods .. one to Encrypt the files, one to decrypt the file and one to see if encryption is already in place (i.e. you don’t want to encrypt an already encrypted section).  So we add these to the button methods in the code behind.

protected void EncryptConfig(object sender, EventArgs e)
{
    if (IsEncrypted(Request.ApplicationPath))
        return;   // alredy done so ignore

   EncryptConnString(Request.ApplicationPath);
    btnDecrypt.Enabled = true;
    btnEncrypt.Enabled = false;
    lblStatus.Text = "The connection string is currently is encrypted ";
    lblConnection.Text = WebConfigurationManager.ConnectionStrings["MyAppConnection"].ConnectionString;
}

protected void DecryptConfig(object sender, EventArgs e)
{
    if (!IsEncrypted(Request.ApplicationPath))
        return;   // alredy decrypted so ignore

    DecryptConnString(Request.ApplicationPath);
    btnEncrypt.Enabled = true;
    btnDecrypt.Enabled = false;
    lblStatus.Text = "The connection string is currently is not encrypted!";
    lblConnection.Text = WebConfigurationManager.ConnectionStrings["MyAppConnection"].ConnectionString;

}

The IsEncrypted, EncryptConnnString and DecryptConString are our methods that do all the work.  Here I’ve implemented them in the page, but there is no reason why it can’t be done in a helper class or within a common framework.

IsEncrypted

Probably the simplest method to understand the “var config” simply opens the configuration file that is passed from the button press.  The path is noted in the ASP.NET request object in a property called ApplicationPath.

private static bool IsEncrypted(string strPath)
{
    var config = WebConfigurationManager.OpenWebConfiguration(strPath);
    return config.GetSection("connectionStrings").SectionInformation.IsProtected;
}

If the of the Section “connectionStrings” has been encrypted a property called IsProtected is set to true.  remember this is all automatic, you don’t have to write any code to check this!

EncryptConnString and DecryptConnString

Here is were it gets a little more complicated, as above the OpenWebConfiguration method will return you an instance of the current web.config file. the commend “section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider")” however; actually does all the work.  The provider is already built into the .NET and will give you 128bit encryption on whatever section you have selected.  In this case it’s the “ConnectionStrings” area.

private static void EncryptConnString(string strPath)
{
    var config = WebConfigurationManager.OpenWebConfiguration(strPath);
    var section = config.GetSection("connectionStrings");
    if (section.SectionInformation.IsProtected) return;
            section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
    config.Save();
}

DecryptConnString is the same as EncryptConnString but calls the UnprotectSection method.

private static void DecryptConnString(string strPath)
{
    var config = WebConfigurationManager.OpenWebConfiguration(strPath);
    var section = config.GetSection("connectionStrings");
    if (!section.SectionInformation.IsProtected) return;
    section.SectionInformation.UnprotectSection();
    config.Save();
}

Run your web application again and press the “Encrypt” button.

image

Notice that the label now says the connection section has been Encrypted; however the connection string is still read correctly!  Now if you open the web.config file using Visual Studio you’ll see the following:

image

Lets see a hacker get past that baby!!

Press “Decrypt” on your application and the web.config will return to :

image

Best of all there is no code to write!!!!!!!

Thursday, August 6, 2009

Using BING Maps in your ASP.NET Applications

I was using the Google Maps API last week on a project and realised that the who JavaScript thing was a bit of a pain to debug.  Although it works fine, it’s fast and is easy to use it still gave me pause for thought.  so looking around for an alternative I decided to give the Microsoft offing a quick once around the block.

Installation

First off you’ll need Visual Studio 2008 to be installed and to down load the Windows Live Tools for Microsoft Visual Studio which contains all the DLLs you’ll need.  It also comes with other bit and pieces, but this post only relates to the mapping element.

After saving the file to your local drive execute the WindowsLiveTools.msi.

image

Click next and follow all the onscreen prompts.

Adding a Map to your site

Microsoft have made it very easy to adding a map to your website, once the software has been installed you get a set of new tools in Visual Studio.

Open up any ASPX Page in design view and select the “Map” icon within the Virtual Earth group on your tool bar:

image

Drag this icon to your' page.

image

Next you need to select a ScriptManager from within the “Ajax Extensions” group and drag this to your page too.

Press Play to debug your application and make sure it’s working.

image

By default you open up over the North American continent which is not the best for an Irish company.  So to fix this we have to go into the page code behind.  Open up the default.aspx.cs file and add the following code:

protected void Page_Load(object sender, EventArgs e)
{
    Map1.Center.Latitude = 53.33306;
    Map1.Center.Longitude= -6.24889;
}

Press Play again and you should see the following:

image

Doing Directions

One of the key advantages to mapping is the ability to get directions from one place to another.  Microsoft have made this really, really easy by just creating a generic list of string with valid addresses.

Create two text boxes on the page with a submit button.

 image

In the code behind add the following to the on click event for the button.

using Microsoft.Live.ServerControls.VE;

protected void btnRoute_Click(object sender, EventArgs e)
    {
        RouteOptions options = new RouteOptions();
        options.SetBestMapView = true;  // will zoom into the route
        List<string> address = new List<string>();
        address.Add(tbxFrom.Text);
        address.Add(tbxTo.Text);
        Map1.GetDirections(address, options);
    }
}

Press play on the application; type the following into the fields and press the “Route” button:

From: East Point Business Park, Dublin, Ireland
To:     O’Connoll Street, Dublin, Ireland

image

Here is the result….  simple and easy…