Problems with Forms Authentication and ASP.NET Themes

It’s sometimes the strangest (and seemingly irrelevant) combinations of technology that give you the most problems. For a good few hours I’ve been wondering why my web site, which uses Forms Authentication and Themes, was not displaying any formatting or images.

It turns out that the URLs specified in the authentication section in web.config are used for ALL resources. So, in my case, the page was trying to load a .css file but ASP.NET was redirecting the request to default.aspx, which of course, is not a style-sheet and has a different MIME type. Here’s the section of my web.config file:

<authentication mode="Forms">
      <forms  loginUrl="Default.aspx"
        defaultUrl="Default.aspx"
        protection="All"
        timeout="30"
        path="/"
        requireSSL="false"
        slidingExpiration="true"
        cookieless="UseDeviceProfile"
        domain=""
        enableCrossAppRedirects="false"/>
    </authentication>

To fix this, I allow unauthenticated access to the resources, which was straight-forward. I added the following to the web.config file:

 <location path="App_Themes">
    <system.web>
      <authorization>
        <allow users="*"/>
      </authorization>
    </system.web>
  </location>

The tricky part in finding this was that the page’s HTML looked fine, there were no errors, and saving the resulting HTML to a file and viewing it produced the right results.

Firefox’s JavaScript Console proved incredibly helpful in finding the problem:

 
Once I saw this, it all fell into place!

BackgroundWorker – automating this handy class

BackgroundWorker objects are an addition to .NET 2.0 to simplify asynchronous programming and are very useful in (but not restricted to) Windows Forms.

The basic idea is:

  • Create a BackgroundWorker object
  • Set-up various events on it, like DoWork, RunWorkerCompleted, and ProgressChanged
  • Set-up various properties, like WorkerReportsProgress and WorkerSupportCancellation
  • Call RunWorkerAysnc

…then, in your form you get notified of progress and can cancel the operation at any time.

I’ve created a ReSharper Live Template that will help you create and set-up the BackgroundWorker object. You can download it here. For instructions on importing the Live Template into ReSharper, see my previous post here.

Visual Studio Build Tip

By default, Visual Studio will build all of the projects in your solution regardless of any errors.

The problem with this is that if the first project it builds contains an error, the resulting binary will not get generated. If any other projects depend on this (which is normally the case, as it was the first to be built) then they’ll fail too, and so on to the end of the project heirarchy.

Pointless; you’ll spend ages sitting around in a locked IDE unable to edit the error until the build finishes.

You might as well stop at the first error. You could of press Ctrl+Break (several times) and hope that VS will eventually stop. But, there’s a better way: get Visual Studio to stop automatically after an error.

  1. Go to Tools/Macros/Macro Explorer
  2. Expand MyMacros
  3. Double click Module1
  4. This will bring up the macro in a new window. In that window, double click the EnvironmentEvents entry.
  5. From the drop-down (currently ‘General’, select ‘Build Events’)
  6. Select ‘OnBuildProjConfigDone’ and paste this in:

If Success = False Then 'The build failed...cancel any further builds.
DTE.ExecuteCommand("Build.Cancel")
End If

The whole method should look like this:

Private Sub BuildEvents_OnBuildProjConfigDone( _  
ByVal Project As String, _ 
ByVal ProjectConfig As String, _ 
ByVal Platform As String, _ 
ByVal SolutionConfig As String, _ 
ByVal Success As Boolean) Handles BuildEvents.OnBuildProjConfigDone   
    If Success = False Then 'The build failed...cancel any further builds.     
        DTE.ExecuteCommand("Build.Cancel")   
    End If 
End Sub

ReSharper Live Templates for validating parameters of public C# methods.

ReSharper Live Templates are smart shortcuts you can type and have filled in automatically. For instance, typing ‘foreach’ and pressing tab produces:

      foreach ( object o in something )
      {
      }

I’ve created 3 Live Templates for validating arguments in public methods (C#). Here’s a short video demonstration. I’ve blogged about these before for ReSharper 1.5 and said that there’s no way to export/import the templates. But in ReSharper 2.0, there is! You can get them here. Go to ReSharper/Options/Templates/LiveTemplates and click the ‘Import templates from file’ button.

MbUnit and null parameters

I’ve recently moved from NUnit to MbUnit.  I like the extra features it offers, in particular, the RowTest feature.  This allows a single test to take different parameters – the parameters of which are specified in the attributes.  Here’s an example:

 

class Test { internal Test( string s ) { if( string.IsNullOrEmpty( s ) ) throw new ArgumentNullException( ) ; } } [ RowTest ] [ Row( @"Hello World!" )] [ Row( @"Another string" )] public void Test1( string val ) { Test t = new Test( val ); }

 

Here, Test1 is being given the parameters from the attributes on the test.  Previously, say, in NUnit, I’d have written a couple of unit tests that create this Test object and give it different values.  Normally, I’d also write a couple that would try and create one with a null string and an empty string and assert that it throws an ArgumentNullException.

Now, in MbUnit, I went to write the test like so:

[ RowTest ] [ Row( null , ExpectedException = typeof( ArgumentNullException ) ) ] [ Row( @"Hello World!" )] public void Test1( string val ) { Test t = new Test( val ); }

Strangely, this caused an internal error in MbUnit.  Reading around, It looks like MbUnit is taking the first parameter of Row and treating it as an array rather than a single parameter.  The complete non-obvious way around this is to cast the null to a string:

 

[ RowTest ] [ Row( (string)null , ExpectedException = typeof( ArgumentNullException ) ) ] [ Row( @"Hello World!" )] public void Test1( string val ) { Test t = new Test( val ); }

This now works.  Which is nice!

ReSharper 2.0.1 Maintenance Release Now Available

 

ReSharper 2.0.1 Maintenance Release Now Available

JetBrains ReSharper version 2.0.1 is now available for download at http://www.jetbrains.com/resharper/download/.
This release fixes a number of bugs and usability issues, in addition to substantial performance improvements. For more information, please see online Release Notes.
The update is a free maintenance release for all licensed users of previous ReSharper versions. All current users are highly encouraged to upgrade, due to the transition of the ReSharper development project to a new issue tracking system. So, the new release automatically sends bugs and exceptions to this new system.
Keep developing with pleasure!

Source: JetBrains .NET Tools Blog » Blog Archive » ReSharper 2.0.1 Maintenance Release Now Available