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…

Tuesday, July 7, 2009

Generating SQL Diagrams using MS Visio 2003

You can build a set of database diagrams using Visio 2003 SP1.  It’s not the best solution but it does work to some extent.  Like all reverse engineering tools it assumes that you have implement some form of relationship mapping and constraints in within the database.

1) Open Visio and Select “Database/Database Model Diagram”

image

2) From the menu choose Database / Reverse Engineer

image

3) Set up your connection to the database

image

choose SQL Server / click New.

image

Select System Data Source and click Next.

image

Select SQL Server and click Next and Finish

image

Enter a meaningful name and enter the DB Server you want to work against (in this case Dubwsdev001), click Next and Next again to use Network Authentication.

image

Select your database and click Next and Finish and then OK.  Click Next at the Reverse Engineering Wizard dialogue.

image

Select the times you want to reverse engineer and click Next.

image

Select the Tables/Views you want to work with and click Finish.

image

You should now see the table in your Visio page. 

Remember that this reflects the table structure exactly, so if there are lots of columns, you’ll see lots of columns.  After a few hours playing around you’ll probably find doing it by hand is easier.

Alternative Solution

You can also use SQL 2005 or 2008 Management Studio to create database diagrams, however we don’t have a copy of that here.

Thursday, June 18, 2009

WCF on Multi Host Servers

I ran into this problem today when I was working on a WCF project.  A project which was working fine on my machine start producing the error “This collection already contains an address with scheme http.  There can be at most one address per scheme in this collection. Parameter name: item

image

Turns out that because the server was hosting a number of different sites using different Host Headers WCF needs to know which of the hosts to respond too.  You can do this by added the following to the services web.config file.

  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true">
    <baseAddressPrefixFilters>
        <add prefix="http://www.blahblah.com"/>
    </baseAddressPrefixFilters>
    </serviceHostingEnvironment>

        ……

   </system.serviceModel>

 

That tells the service to only respond on the specific header.

Wednesday, June 17, 2009

Self signing your server for SSL

I’ve been playing around with WCF and one of the problem areas is that our secure servers use a reverse-proxy configuration to route requests for web services.  This is all fine for old web services; however WCF is a little more particular with it’s requirements.  The reason for this is that the SSL Certificate does not actually exist on the Secure Server, it exists on the Proxy server, therefore when WCF starts it brings up the error saying “SSL is not supported”.  To resolve this we need to provide the server with a mock certificate using the same domain name as that used by the Proxy server.

To create a local mock SSL certificate for your development machine is quite easy and is also very useful to have if you are doing testing.

Installation

  1. Download the Internet Information Services (IIS) 6.0 Resource Kit Tools from the Microsoft site.
  2. Run the installation  image

         image

3.  Choose a complete install and follow all the instructions until it’s finished.

4.  Now you go to your Start/Programs/IIS Resources/SelfSSL/SelfSSL.

      image

5.   You need to work out your Site ID and the easiest way to get this is to use IIS Admin right click the website properties and go to Logging properties in the website Tab and look at the Log File name below the Site ID is “1”.

       image

6.   Enter the command “SELFSSL.EXE /S:1” and confirm “Y”.

      image

7.   Your site is now activated to use SSL.

     image

Monday, June 8, 2009

SharePoint Site Templates

I was recently asked to change the basic template used when creating a site for the first time from a Blank Site to Team Site.  As the site was created using a PowerShell script it required finding out which code should be used for the sitetemplate option in the STSADM command.

# Create new WebSite
stsadm.exe -o createsite -url $spwebsite -title "My Team Site" -ownerlogin entirl\$username -owneremail sharepointadmin@enterprise-ireland.com -sitetemplate STS#1

Friendly Name Template
Team Site STS#0
Blank Site STS#1
Document Workspace STS#2
Wiki Site WIKI#0
Blog BLOG#0
Basic Meeting Workspace MPS#0
Blank Meeting Workspace MPS#1
Decision Meeting Workspace MPS#2
Social Meeting Workspace MPS#3
Multipage Meeting Workspace MPS#4
Document Center BDR#0
Records Center OFFILE#1
Personalization Site SPSMSITE#0
Site Directory SPSITES#0
Report Center SPREPORTCENTER#0
Search Center with Tabs SRCHCEN#0
Search Center SRCHCENTERLITE#0
Publishing Site CMSPUBLISHING#0
Publishing Site with Workflow BLANKINTERNET#2
News Site SPSNHOME#0