Monday, June 15, 2015

SSL Testing with ASP.NET MVC

Test your ASP.NET MVC or WebForms Application on IIS 7 in 30 seconds



A frequent complaint in the ASP.NET forums is that an application works fine with Cassini (AKA, the built in Visual Studio Development Server), but fails when deployed to a real IIS server. The two most common reasons for this failure are:
  1. Cassini runs at the root level, so when resources (such as CSS and JavaScript files) are referenced incorrectly, they still work. For example, create a new MVC 3 Application and change the CSS and jQuery as shown below:

    @*
       <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
        <script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
    *@    <linkhref="/Content/Site.css""rel="stylesheet"type="text/css" />
        <
    scriptsrc="/Scripts/jquery-1.5.1.min.js"type="text/javascript"></script>The application continues to work. But when you run it under IIS, client side validation no longer works and you don’t have CSS styling. The application looks unstylish:

    NoCSS

    This problem with assuming root access to the resources is so common that most ASP.NET MVC books and many ASP.NET MVC tutorials have samples that don’t work on IIS.
  2. Cassini runs under the logged in users credentials, that’s you, and you're often running as administrator. IIS runs under the application pool identity, and for security reasons has far less access than an administrator. Access to any resource ( a remote SQL Server, a local file ) often fails when run on IIS.
IIS Express is a huge improvement over Cassini, but it doesn’t solve either of these problems. IIS has additional advantages over IIS Express:
  1. Easier to test your application from remote computers.
  2. Easier to set up and test SSL.  See Better, Faster, Easier SSL testing for ASP.NET MVC & WebForms
Since IIS is tied to the OS, you need Vista/SP1 or higher to test with IIS 7. ( Win7/SP1 strongly preferred. )

Installing IIS 7

  1. Run the Web Platform Installer (WPI)
  2. In the left pane select All. In the tabs select Products.
  3. Install IIS 7 Recommended Configuration.WPI
  4. In the Start menu, type IIS, and select Internet Information Services (IIS) Manager.

    Start_IIS

    IIS manger is displayed.

    IISmgr

Create a new ASP.NET MVC 3 application or Web Application, accept all the defaults. If you want to follow this tutorial, name it MvcFun.
newMVCproj
 WARNING: IIS cannot run an ASP.NET project that has been created in the default Visual Studio project folder (C:\users\<user>\Documents\Visual Studio 2010\Projects). Select a folder where IIS can access the files, such as C:\Webs.
Right click the solution and select Properties.
RightClickProject
  1. Select Web in the left pane.
  2. Under Servers, select the Use Local IIS Web server radio button.
  3. Select the Create Virtual Directory button.
UseLocal_IIS_sm
Should you get the message:
Unable to create the virtual directory. To access Web sites on the local IIS Web server, you must run Visual Studio under an Administrator account.
RunAsAdmin
Read it and follow the directions and you’ll be rewarded with a friendlier message.
vid_Success
Now go back to IIS manager, refresh and drill down in the Default Web Site. Select Browse *:80(http) in the right pane.
IISmgrBrowse80
If you get the following Parser Error,  you need to build your application.

Server Error in '/MvcFun' Application.

Parser Error

Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.
Parser Error Message: Could not load type 'MvcFun.MvcApplication'.
Source Error:
Line 1:  <%@ Application Codebehind="Global.asax.cs" Inherits="MvcFun.MvcApplication" Language="C#" %>
Source File: /MvcFun/global.asax Line: 1

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.225

Testing your Application

After building, refresh  your browser. In the browser URL, change localhost to the name of your computer. In the image below, I’m using the Q3 computer.
Q3
Go  to another computer and test the URL.  If you cannot access the Web application from a remote computer, you might need to configure the firewall on the computer that is serving the Web pages.

To enable the Web server access through the Windows firewall

  1. In the Windows Search programs or files box, enter "firewall".
  2. Select Allow a program or feature through Windows Firewall.
  3. Click Change settings and scroll to the bottom.

  4. Select World Wide Web Services (HTTP).
  5. Verify that the URL for your ASP.NET MVC 3 application now works from a remote computer.

Article was taken at:
http://blogs.msdn.com/b/rickandy/archive/2011/04/22/better-faster-easier-ssl-testing-for-asp-net-mvc-amp-webforms.aspx

No comments: