Installing PositiveSSL SSL certificate on IIS/Windows Azure (ASP.Net MVC)



I still don’t understand various concepts behind certificates like SSL certificate, code signing certificate etc. I only know that they must be used for security & privacy. Each time I have to renew the certificate on our Notezilla.Net server that is used by our sticky notes app Notezilla, I forget how I did it last time as there are many steps involved.

Last time I used GoDaddy’s SSL certificate. This time I went for PositiveSSL SSL certificate from NameCheap.com as this was the cheapest I found (very cheap compared to GoDaddy).

I looked over the Internet to find how to install PositiveSSL certificate on Windows Azure (IIS) & configure my ASP.Net MVC project for HTTPS. I found the information all scattered over different websites. Spent lot of hours setting it right. Basically, I have unified all the steps in this single post.

Step 1

Buy PositiveSSL SSL certificate from NameCheap.com. This is the certificate that will only require your domain to be verified.

Step 2

Once you purchase the certificate, you will need to activate it from your NameCheap account settings. Activation requires you to generate a CSR from IIS. You can do it from any Windows machine which has IIS installed. You can learn how to generate CSR (certificate request) on IIS.

CSR is saved into a text file. You would copy this text and give it to NampCheap when activating the certificate. NameCheap will now check if you are the owner of the domain for which you are requesting the certificate. Once you complete the verification, Comodo will send you the .cer file (certificate) as an attachment to an email.

Step 3

Now, you would ensure that you have the root & intermediate certificates installed on the system where you generated the CSR. To install these, refer to this page. You would need to download these certificates from either this link or this link.

Typically, there is one root & 2 intermediate certificates. Pick the latest from above link.

Step 4

After you have installed the root & intermediate certificates as in previous step, you would now install the .cer file for your domain that Comodo sent to you. To do this, follow the Complete Certificate Request link in IIS . You don’t have to follow the bindings steps given in latter link if this is not your final server system.

When doing the above, you may encounter an error “Cannot find the certificate request associated with this certificate file. A certificate request must be completed on the computer where it was created”. To resolve this, look here.

Step 5

In order to upload all the root & intermediate certificates to Windows Azure, you will need to first export those certificates from IIS (where you imported them) to .cer files because this is what Windows Azure wants. Do this from the Certificates Management Console on your IIS machine.

Step 6

Now export your domain certificate into a .pfx file. Go to Personal->Certificates in the Certificates Management Console to export.

Step 7

At this point, we have 2 to 3 .cer files and a .pfx file. Go to your Windows Azure portal, select the cloud service, click on the ‘Certificates’ tab and upload all the exported certificates. Note, in one of the Firefox versions I was not able to upload the files to Windows Azure. I then used Internet Explorer to upload the files.

Step 8

You would now need to configure your ASP.Net Cloud Service project to use these certificates. Right click on the Web role in your cloud service project and choose ‘Properties’. Under ‘Properties’, click on Certificates option. Add your certificate information. Look at the first 3 rows below. Ignore the last row.

Cloud Service Project - Certificates Properties

Step 9

Once you do this, configure an endpoint for HTTPS. Right click on the Web role in your cloud service project and choose ‘Properties’. Under ‘Properties’, click on Endpoints option. See below.

Cloud Service Project - Endpoints Properties

Step 10

Build your package & deploy it on Windows Azure. It should work. You may also want to redirect all HTTP requests to HTTPS. I have done this for Notezilla.Net.



Visual Studio closes/crashes when opening edmx file



Whenever I tried opened the Entity Framework’s edmx file, Visual Studio 2008 would simply close. I tried to google, stack overflow. But couldn’t find the answer.

I noticed that when I double clicked on the edmx file, the status bar said “Initializing toolbox” just before closing of Visual Studio.

SOLUTION

  1. Close all the open windows using the Window->Windows menu in Visual Studio
  2. Explicitly click on the “Toolbox” pane. From the menu you would choose View->Toolbox.
  3. Then double click on .edmx to open it. You should be able to open it successfully.

I still unsure whether .edmx opened because I closed all the windows or because I opened the Toolbox explicitly.

Thanks :)



Adding Razor support to existing ASP.Net MVC 3 project



Follow the below steps to add Razor support to existing ASP.Net MVC 3 project

  1. Create another Temporary project based on MVC 3 Razor support by default
  2. Compare the references, web.config (main one and one under views folder) in the Temporary project with my own project. Here I found references to System.Web.Helpers & System.Web.Webpages. Similar mentions exist in the web.config files also. Just copy those sections from the temporary project to your project.
  3. Copy the _ViewStart.cshtml file from temporary project to your project (under Views folder)
  4. Copy the _Layout.cshtml file from temporary project to your project (under Views\Shared folder)
  5. Also check inside _Layout.cshtml. It may be referencing other partial views, jquery (js file) etc. Make sure they all exist in your own project.
  6. That’s it. Compile and all should be set right.

Thanks :)



microsoft.data.entity.design.extensibility could not be loaded when opening edmx



I received this error when opening an edmx file in my ASP.Net MVC 3 project. Spent several hours to figure out. Another related problem I had was that the “ADO.Net Entity Data Model” project template did not appear when adding new item to the project. Spent several hours finding a solution & finally found it here: http://stackoverflow.com/questions/5696436/no-entity-data-model-edmx-template-with-visual-studio-2010-sp1-ado-net-entit

Installing “ADO.NET Entity Framework Tools” from Visual Studio 2010 media solved both the problems mentioned above.

Thanks :)



Localization in ASP.Net MVC Projects



Hello all,

As I am new to .Net Framework & ASP.Net MVC, I was looking in to various articles that would give clear step by step instructions as to how to add localization to ASP.Net MVC Projects. Especially, localization of string resources.

Here is a fantastic article on Localization in ASP.Net MVC projects.

Thanks to the author.



HTTP Test Client



When developing a RESTful Web service in ASP.Net MVC I was looking for a HTTP Test client that I can be used to test the RESTful API that I had created. It is easy to test HTTP GET with a browser. What I was looking for in the tool was the ability to HTTP POST/PUT/DELETE to any Url. Thanks to Stackoverflow where chris recommended a fantastic freeware tool called Fiddler

Definition from Fiddler’s website: Fiddler is a Web Debugging Proxy which logs all HTTP(S) traffic between your computer and the Internet. Fiddler allows you to inspect all HTTP(S) traffic, set breakpoints, and “fiddle” with incoming or outgoing data. Fiddler includes a powerful event-based scripting subsystem, and can be extended using any .NET language.

Thanks.



RESTful Web Service Using ASP.Net MVC



Piers Lawson wrote excellent series of articles to guide you to write RESTful web service using ASP.Net MVC framework.

There are several parts (24 at the time of writing this post). But I think the first 12 parts are good for beginners to start with.

Part 1Part 2Part 3Part 4Part 5aPart 5bPart 6Part 7Part 8Part 9Part 10Part 11Part 12

Thanks.



Redirecting old urls to new in ASP.Net MVC



Recently I migrated my classic ASP website to ASP.Net MVC. However, there were many websites still linking to specific .asp pages that no longer exist. I looked for some help on Google so that I could easily redirect the legacy URLs to the new one.

Thanks to Mikesdonetting post that gave me that ‘real code’. You will need to read this post for clear understanding.

While the above post covers the most important aspect of redirecting (LegacyUrlRoute class), I have modified the code so that it suits a more common scenario.

Here is the code:

namespace www.Helpers
{
    class RedirectRule
    {
        public string OldUrlContains;
        public string OldUrlContainsNot;
        public string NewUrl;
 
        public RedirectRule(string strOldUrlContains, string strOldUrlContainsNot, string strNewUrl)
        {
            OldUrlContains = strOldUrlContains;
            OldUrlContainsNot = strOldUrlContainsNot;
            NewUrl = strNewUrl;
        }
    };
 
 
    public class LegacyUrlRoute : RouteBase
    {
        RedirectRule[] RedirectRules =
        {
            new RedirectRule("Notezilla/default.asp", "", "/Notezilla"),
            new RedirectRule("RecentX/default.asp", "", "/RecentX"),
        };
 
 
        public override RouteData GetRouteData(HttpContextBase httpContext)
        {
            const string status = "301 Moved Permanently";
            var request = httpContext.Request;
            var response = httpContext.Response;
            var legacyUrl = request.Url.ToString();
            var newUrl = "";
 
            foreach (RedirectRule rule in RedirectRules)
            {
                //if we don't have to check for a string that does not exist in the url
                if (rule.OldUrlContainsNot.Length == 0)
                {
 
                    //this does a case insensitive comparison
                    if (legacyUrl.IndexOf(rule.OldUrlContains, 0, StringComparison.CurrentCultureIgnoreCase) >= 0)
                    {
                        newUrl = rule.NewUrl;
                    }
                }
                else
                {
                    //if we don't have to check for a string that does not exist in the url
                    if ((legacyUrl.IndexOf(rule.OldUrlContains, 0, StringComparison.CurrentCultureIgnoreCase) >= 0)
                        //so that it doesn't go in infinite loop since the end part of both urls are same
                        && (!(legacyUrl.IndexOf(rule.OldUrlContainsNot, 0, StringComparison.CurrentCultureIgnoreCase) >= 0)))
                    {
                        newUrl = rule.NewUrl;
                    }
                }
 
                //found anything?
                if (newUrl.Length > 0)
                {
                    break;
                }
 
            }
 
 
            if (newUrl.Length > 0)
            {
                response.Status = status;
                response.RedirectLocation = newUrl;
                response.End();
            }
 
            return null;
        }
 
        public override VirtualPathData GetVirtualPath(RequestContext requestContext,
                    RouteValueDictionary values)
        {
            return null;
        }
    }
 
 
}

Here is the explanation:

RedirectRule is a class that holds 1) the old url substring that we much check for, 2) the old url substring that should not exist in the url and 3) the new url which the user must be redirected to.

We initialize an array of such RedirectRule objects and check (inside the function GetRouteData) if the active url matches the criteria given in each rule. If it matches then we redirect to the new url.

Let us see, when we will have the case where RedirectRule.OldUrlContainsNot will need to be used.

Imagine that the old Url was ‘Notezilla/NotezillaSetup.exe’ and the new Url is ‘/Downloads/Notezilla/NotezillaSetup.exe’. In our case if we just check for the old Url then we will end up in infinite loop since the end part of both old and new Url is same (Notezilla/NotezillaSetup.exe). For this reason we need OldUrlContainsNot. In this case we will set OldUrlContainsNot to ‘/Downloads’ so that we do not get in to infinite loop.

Once you are done with the above code just add LegacyUrlRoute to existing routes in Global.asax.cs

 
public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
 
    routes.Add(new LegacyUrlRoute());
}

That’s it.

Thanks.



Redirecting .asp to aspx in ASP.Net MVC



I am learning ASP.Net MVC and I am loving it.

In the process of migrating our existing classic asp website to ASP.Net MVC we realized that we would still need an .asp file as part of the web app. It’s the redirect.asp file that all our products call to go to different parts of our website.

Fortunately, ASP.Net MVC provides a neat way to do this without actually creating an .asp file. You can route the .asp call to .aspx (MVC View) and have all your code in ASP.Net MVC controller.

Go to Global.asax.cs and write the following code:

routes.MapRoute("Redirect", "redirect.asp", new { controller = "Home", action = "Redirect" });

That’s it.

Note that the redirect.asp file should not (and does not) exist. The routing engine would just route the call to an action that you specify.

Thanks.