Debug SOAP and REST in IIS Express using Fiddler

I’ve been using Visual Studio 2013 Express to make a C# website that uses SOAP to request services from another company.  Everything was working fine, until one day I suddenly started getting HTTP 500 errors.  Naturally the service provider said it wasn’t them, so out came Fiddler.  Funnily enough the request worked straight away in Fiddler, so I needed to find out what the difference was between a Fiddler request and a request from my localhost.

I followed this guide here: http://docs.telerik.com/fiddler/Configure-Fiddler/Tasks/ConfigureDotNETApp

However:

GlobalProxySelection.Select = new WebProxy(“127.0.0.1”, 8888);

should now be:

WebRequest.DefaultWebProxy = new WebProxy(“127.0.0.1”, 8888);

Because of this issue, we can’t debug simply using ‘localhost’.  So I used this guide to use a custom host name as opposed to ‘localhost’ – using the following locations as reference for the applicationhost.config file:

Pre-Visual Studio 2015

Visual Studio 2015+

In Fiddler, because I’m using HTTPS, I had to go to Tools > Fiddler Options > HTTPS tab and check ‘Decrypt HTTPS traffic’

And finally I needed to add this line BEFORE my C# HttpWebRequest code for the request to work properly when Fiddler was running:

ServicePointManager.ServerCertificateValidationCallback +=(sender, cert, chain, sslPolicyErrors) => true;

I think that’s it…..

ASP.Net Upload Handler using SSH.Net and Plupload

Ok.  I won’t drag this out too much since there’s FAR too much to comment on.  But my aim was to create a web-based upload facility that is compatible with old browsers (such as IE8) and has the ability to upload over SFTP (ASP.Net doesn’t natively support this via the FtpWebRequest object).

So in summary I’ve used:

Plupload – This is the upload widget, which degrades nicely from HTML5 down to Flash, Silverlight and HTML4.  (note that Flash doesn’t support chunking of uploads)

SSH.Net – This is the free library I used to upload via SFTP.  It really is brilliant.

Here is an excerpt of the ASPX page containing Plupload.  Things to note:

  • It supports uploading of zip,rar,7z,iso file types only
  • It reports upload speed
  • It uses a default chunk size of 10mb (where chunking is supported)
  • Points to an upload handler called FileUpload.ashx
  • Passes 3 parameters to the upload handler – client, applicationID and uploadType
  • Has a button to reset the uploader
  • Informs user to use a newer browser for better performance (based on Plupload render type)

And here is the upload handler, FileUpload.ashx.  I’ve stripped some logic out where I obtain things like ftpPassword etc (I obtain these in reality by running a CAML query on a SharePoint list).  When I obtain this encrypted password from SharePoint (based on the ‘client’ parameter passed in from Plupload), I decrypt it using the Rijndael decryption method here.  Also, AlkaneLogging is just a logging class I wrote which writes debug messages to a text file.  Comment these lines out if needs be:

Rijndael Encryption and Decryption in C# and Powershell

This is an example of how we can perform Rijndael encryption and decryption in C# and Powershell. It’s worth mentioning that:

– salt must be 8 bytes minimum
– initVector must be 16 bytes minimum

The examples below use Cipher Block Chaining (CBC), zero padding, 10000 key iterations and 32 byte (256/8) block cipher.

Powershell:

C#

and call using:

Getting Public Key Token of Assembly with Powershell

When I need to add definitions to the web.config of my ASP.Net projects I often use this Powershell line to get the PublicKeyToken, Culture and Version etc.

Running this will yield a similar output to the following:

AlkaneSolutions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=aa13051522563c02

Integrating Lync Status into an ASP.Net Gridview

This post describes integrating Lync status into an ASP.Net Gridview.  I had a bit of a nightmare trying to achieve this so that it worked on IE8 all the way up to IE11 .  First I had it working on IE8 but for IE9+ the status always showed as offline.  I finally made it work using the example below (using Lync 2010):

First create the Gridview – in this example we’re binding ‘analysts’ :

You’ll notice that the ID of the label is called lyncStatus, which will obviously generate a unique ID (for example ‘analystGV_ctl01_lyncStatus’, ‘analystGV_ctl02_lyncStatus’ etc) for each label when the Gridview renders.  We also use a data-id attribute to store the email address of the analyst.  I’ve seen examples in the internet where people have used the SIP URI as the ID on fields, but this won’t work in a Gridview if we’re expecting multiple instances of the same SIP (the same user, for example) and as the ID would suggest, it would not be unique!

The Text of the label displays the analyst’s name.

Now paste the following into the <head> section:

Above we include the JQuery library and specify some styles for the status of each user.

Here are the icons I use:

 

Finally, just before the </body> tag, paste this:

When the document has loaded, this piece of JQuery iterates through all elements with an ID ending with ‘lyncStatus’.  it retrieves the id and the data-id attributes (the id so it knows what element to set the Lync status on, and the data-id which stores the SIP URI (email address in my case)).  If an instance of the nameCtrl object was successfully created, and presence is enabled, it attempts to get the status for that SIP.

**Note**

I experienced intermittent issues where duplicate users would not always be rendered correctly.  That is, when GetStatus was called for a duplicate user (let’s say the same user joe.bloggs@alkanesolutions.co.uk appears twice) it would not return the correct value and OnStatusChange would never be called.  To circumvent this issue, when the first instance of each user is ‘rendered’, I then use JQuery to find all other users with the same SIP URI and to give them the same status css class.  Not ideal, but it works!

**Another Note**

If you’re testing this on an x64 platform, you must launch the x86 version of Internet Explorer.  Otherwise it appears we cannot instantiate the Name.NameCtrl object, and we will get the ‘Automation server can’t create object’ error in our ‘catch’ block.

Also if you have Lync 2013 (and not 2010 like me) you may be able to get the integration working in Chrome/Firefox too by following instructions on how to instantiate the object here.

Create a custom debug log file in an ASP.Net Class

This is a quick example of how to create a custom debug log file in an ASP.Net class which you can use in your website.  If the file doesn’t exist, it creates it.  It names the file today’s date.  And records a date/time at the start of each log entry

.aspx.cs page

 

.cs class

 

Getting and Setting Session State in HttpHandlers (ASHX files)

If you just want to read your Session State from an ASHX or HttpHandler, you need to implement IReadOnlySessionState.  If you want to write to your Session State, you must implement IRequiresSessionState.  Note that you will also need use the System.Web.SessionState namespace.  This post describes getting and setting session state in HttpHandlers (ASHX files).

 

Strip out style attributes in HTML

This post describes the process I use to strip out style attributes in HTML code using a regular expression.

My website is presenting data from a field in SharePoint.  This field uses HTML and CSS style attributes to construct the note.  A user would enter this data via a Sharepoint website, and my .Net website will present it elsewhere. The trouble is, when my site presents this data the message can look like a right mess.  Different fonts, different sizes and different colours (you’ve met those idiots before who like to use Comic Sans font in a professional environment, right?).  So before I present the data in a Literal control I decided to write a regular expression to strip out any style/class attributes etc.  And here is the .Net function (which I have in a class):

It won’t produce perfect results, because there are also uses of the <font> tag scattered about in these messages.  But I’m going to leave those alone for now since I suspect <font> tags may be used to highlight (bold/colour) certain words (auto-generated from the WYSIWYG editor).

Re-bind the GridView on the Parent Page after FancyBox close

I have a GridView with hyperlinks in some of the columns, which open up a new page in a FancyBox.  FancyBox is a pretty cool lightbox alternative.  Anyway, in the FancyBox page content you can make changes which will affect values in the parent page’s GridView.  So when the FancyBox is closed, I needed to refresh the parent GridView.  Here is how you can re-bind the GridView on the Parent Page after FancyBox close:

First I create a button, gave it an OnClick event, and used CSS to make it hidden:

FancyBox has a handy afterClose function which, unsurprisingly, gets calledafter the FancyBox is closed.  What it does in here is call the click event of my hidden button:

and then of course, the code behind for the button click event should rebind the gridview!

 

Detect if an ASP.Net form is Valid from Javascript

We can detect if an ASP.Net form is valid from Javascript by using the following: