Solutions For Async/Await In MVC Action Filters

15 August 2017

Async/await has been available in .net for years but until the release of asp.net core there was no way to create a MVC ActionFilter that uses async/await properly. Since async was not supported by the framework there was no truly safe way to call async code from an ActionFilter. This has changed in asp.net core but if you are using ASP.Net 5 or below you’re stuck.

Recently, I found a workaround to using an async HttpModule to load whatever data the ActionFilter will need. You could also do all the work of the ActionFilter in the HttpModule but I prefer to keep the filter because it ties more closely into the rest of the MVC pipeline. My example will demonstrate moving async code out of an AuthorizationFilter but the pattern will work with any ActionFilter.

ActionFilter to fix

This is an example authorization filter that does async work as part of the authorization of the request. Because attributes do not have async methods to override we’re stuck calling .Wait() and .Result to synchronously execute the task. This code is ripe for deadlocks.

public class WebAuthorizationFilter : AuthorizeAttribute
{
  public override void OnAuthorization( AuthorizationContext filterContext )
  {
     if ( AllowAnonymous( filterContext ) )
     {
        return;
     }

     Task<bool> isAuthorizedTask = DoAsyncAuthorizationWork( filterContext.HttpContext );
     isAuthorizedTask.Wait();

     bool isAuthorized = isAuthorizedTask.Result;
     if ( !isAuthorized)
     {
        filterContext.Result = new UnauthorizedResult();
     }
  }
}

There are 2 classes necessary to add the module:

New HttpModule to handle async code

Any async code goes in this class. Call necessary methods then add state to HttpContext.Items.

public class WebAuthorizationAsyncModule : IHttpModule
{
    public void Init( HttpApplication context )
    {
       var authWrapper = new EventHandlerTaskAsyncHelper( AuthorizeRequestAsync );

       // Execute module early in pipeline during request authorization
       // To execute the module after the MVC route has been bound, use `context.AddOnPostAcquireRequestStateAsync` instead
       context.AddOnAuthorizeRequestAsync( authWrapper.BeginEventHandler, authWrapper.EndEventHandler );
    }

    private static async Task AuthorizeRequestAsync( object sender, EventArgs e )
    {
       HttpApplication httpApplication = (HttpApplication) sender;
       HttpContext context = httpApplication.Context;

       bool isAuthorized = await DoAsyncAuthorizationWork( context );

       // Store the result in HttpContext.Items for later access
       context.Items.Add( "IsAuthorized", isAuthorized );
    }
}

Module Registration Startup Class

This class registers the HttpModule created above with asp.net. You can also register in the web.config but I prefer to keep this kind of configuration in code.

public class PreApplicationStartCode
{
  public static void Start()
  {
    DynamicModuleUtility.RegisterModule( typeof( WebAuthorizationAsyncModule ) );
  }
}

The second code change required is to add the PreApplicationStartCode class to the startup classes registered with asp.net. To do this use the PreApplicationStartMethod attribute on your HttpApplication class in Global.asax.cs.

[assembly: PreApplicationStartMethod( typeof( Some.Code.PreApplicationStartCode ), "Start" )]
namespace Some.Code
 {
    public class WebApplication : HttpApplication
    {
      ...

Action Filter changes

This is the same authorization filter from above changed to read the authorization result from Httpcontext.Items instead of doing the work directly.

public class WebAuthorizationFilter : AuthorizeAttribute
{
  public override void OnAuthorization( AuthorizationContext filterContext )
  {
     if ( AllowAnonymous( filterContext ) )
     {
        return;
     }

     bool isAuthorized =  (bool) filterContext.HttpContext.Items["isAuthorized"];
     if ( !isAuthorized)
     {
        filterContext.Result = new UnauthorizedResult();
     }
  }
}

Next Steps

This example was deliberately simple and as such it executes for every request. To only execute the module for specific URLs you can inspect the HttpContext.Request.Url property. Or if you delay execution of the module until after the ‘Acquire State’ pipeline step in asp.net (context.AddOnPostAcquireRequestStateAsync in Module.Init) and access the MVC route values using HttpContext.Request.RequestContext.RouteData.Values you can only execute the module code for specific Controllers/Actions in MVC.

--

Effective logging in .Net

27 July 2017

Logging is one of those things that everyone says you should be doing but it’s hard to get it right. Good logging can be the difference between finding and fixing a bug in a few hours or that same fix taking days and additional releases to isolate a problem. In this post I’ll lay out my thoughts on logging and how to do it well. Hat tip to Nicholas Blumhardt for introducing many of these ideas to me.

Where are we?

The first question to ask is “what kind thing am I building?”. If the answer is a .net application or website then you can skip this section. If you’re building a reusable library then you should consider an abstraction to let the consumer of your library decide how to log. The .net ecosystem has many options for logging abstraction but most require you to include a reference to the library in your dependency tree. For a cross cutting concern like logging you may run into libraries that use different logging abstractions or potentially conflicting versions of the same abstraction. For this reason I don’t like solutions that require a dependency on an external library. I also would prefer to not reinvent the wheel and create a new logging abstraction in each project I work on.

Enter LibLog, “a single file for you to either copy/paste or install via nuget, into your library/framework/application to provide a logging abstraction.” LibLog is just a source file that is included in your library that adds an internal ILog abstraction. Consumers of your library do not have to care about it because it also includes transparent support for common logging providers. Using reflection LibLog finds the logging framework your application is using and logs to it, without the consumer having to write any code.

One last piece of advice when adding logging a library, make sure you keep the log levels low. It’s unlikely you need to log above a Debug level. Instead of logging with Warning or Error levels, communicate the problem in an obvious way using an exception or error code. Even Information logs should be kept to a minimum.

Application Logging

The more common scenario for developers is the need to log inside of an application like a desktop app or website. For that I prefer structured logging as opposed to text logging. Structured logs rather than simply being a string of text are a set of key-value properties with a timestamp. Where you might have a text log of "12-17-2016 - Logging in as user with id '1234' failed.", a structured log would look like '{ timestamp: '12-17-2016', UserId: 1234, action: 'log_in' status: 'failed'}'. The structured log conveys the same information (and can even be rendered as a string) but has the advantage of being queryable if stored in a log server that supports structured logs. Rather than searching for a string, you can quickly find all log_in actions with a status of failed using a query. Application Insights and Seq are two examples of logging servers that support structured logs.

Serilog is my preferred library for writing structured logs. Serilog pioneered an easy way to write structured logs in .net. Rather than providing an object, you provide a message template similar to a string.Format template that looks something like Log.Information("{Action} finished with {Status} for {UserId}", Actions.LogIn, Statuses.Failed, userId);. Serilog can then convert that log into a string like "action 'log_in' finished with status 'failed' for user id '1234'" or if your logging server supports it, a log object like

{
    "time": "2016-12-17",
    "level": "Information",
    "template": "{Action} finished with {Status} for {UserId}",
    "properties": {
        "action": "log_in",
        "status": "failed",
        "userId": "1234"
    }
}

Structured logging using message templates are also supported directly in .net core using the Microsoft.Extensions.Logging library. More information on structured logging can be found in Nicholas Blumhardt’s blog series.

Correlation

When trying to track down an issue with a production application, it’s hard to tell which logs go with a given request unless you tie them together. This is commonly called a CorrelationId. Serilog supports adding it to all messages via LogContext -

using (LogContext.PushProperty("CorrelationId", Request.Id))
{
  // All logs inside the context will have the CorrelationId added to them
}

Microsoft.Extensions.Logging also supports adding properties to all messages through ILogger.BeginScope(). ASP.Net core will even add this by default. You can even use a library like CorrelationId to ensure a CorrelationId is passed among services if you are running in a microservice environment where you want to track a request across services. In addition to tracking CorrelationId, things like Environment and MachineName can be helpful when troubleshooting.

Keep it DRY

Rather than sprinkling Log.Information(...) calls throughout your codebase I suggest creating a simple, generic class to encapsulate the logger (Credit to Erik Heemskerk for the idea) -

public class ApplicationMonitoring
{
  // from serilog
  ILogger Logger { get; }
  ILogContext LogContext { get; }
}

This class is deliberately kept simple so that it can be shared widely throughout your application’s code base. Then extend the class with whatever logging needs you might have using extension methods on the ApplicationMonitoring class -

public static class UserActionsApplicationMonitoring
{
   public static void UserLogInFailed( this ApplicationMonitoring monitoring, int userId )
   {
       monitoring.Logger.Information("{Action} finished with {Status} for {UserId}", Actions.LogIn, Statuses.Failed, userId);
   }

   public static void UserLogInSucceeded( this ApplicationMonitoring monitoring, int userId )
   {
       monitoring.Logger.Debug("{Action} finished with {Status} for {UserId}", Actions.LogIn, Statuses.Success, userId);
   }
}

This way the details of how you log are separate from your business logic. Additionally, if you want to add metrics tracking of your monitoring events it’s easy to add to the existing classes. Add an IMetrics property to your ApplicationMonitoring class then call it from the existing methods. For example, to track the count of failed logins change the UserLogInFailed method to -

   public static void UserLogInFailed( this ApplicationMonitoring monitoring, int userId )
   {
       monitoring.Logger.Information("{Action} finished with {Status} for {UserId}", Actions.LogIn, Statuses.Failed, userId);
       monitoring.Metrics.IncrementCounter( Counters.LogInFailed );
   }

The last thing that’s necessary for effective logs is a way to change logging levels on the fly to tap into extra detail for troubleshooting a production issue. How to do so is outside the scope of this post but it should be easy to do and should not require a restart of the application.

I hope this post gave some clarity on effective logging in .net. Logging is an incredibly effective tool for troubleshooting a production application but it takes work. Instead of leaving it to the end of your project, I suggest laying the groundwork for logs in the beginning to set yourself up for success. Make it easy to write good logs to help developers on your project “fall into the pit of success”.

--

Implementing 'Keep Me Signed In' in Windows Identity Foundation

04 February 2014

A common feature of website authentication is the ‘Remember me’ or ‘Keep me signed in’ option. This feature is not a built-in feature of Windows Identity Foundation. The easiest solution is to make all Relying Party cookies Session cookies, meaning they expire when you close the browser. When you navigate back to the relying party you’ll be sent to the STS, automatically logged in and sent back. This can be a pain for a number of reasons so it’s ideal if we can setup the Relying Party cookies the same as the STS. I’ll show how it can be implemented using claims as the means of communication between the STS and Relying Party.

The STS setup

To communicate whether or not the user wanted to be remembered, we’re going to use claims. Specifically we’ll be using two existing claims from the Microsoft.IdentiyModel.Claims namespace, IsPersistent and Expiration. To do so, first add the claims to the FederationMetadata xml so you see something like this:

<auth:ClaimType xmlns:auth="http://docs.oasis-open.org/wsfed/authorization/200706" Uri="http://schemas.microsoft.com/ws/2008/06/identity/claims/ispersistent" Optional="true">
   <auth:DisplayName>isPersistent</auth:DisplayName>
   <auth:Description>If subject wants to be remembered for login.</auth:Description>
</auth:ClaimType>
<auth:ClaimType xmlns:auth="http://docs.oasis-open.org/wsfed/authorization/200706" Uri="http://schemas.microsoft.com/ws/2008/06/identity/claims/expiration" Optional="true">
   <auth:DisplayName>Expiration</auth:DisplayName>
   <auth:Description>How long before the persistent session cookie should expire</auth:Description>
</auth:ClaimType>

As the description states, we’ll be using the IsPersistent claim to communicate if the user wanted to be kept logged in and the Expiration claim to communicate the session expiration if IsPersistent is true.

The last step on the Relying Party is to set the claims on the user’s principal. Update the IClaimsPrincipal creation code to specify the two new claims.

public static IClaimsPrincipal CreatePrincipal( UserModel user, bool rememberMe )
{
  if ( user == null )
  {
    throw new ArgumentNullException( "user" );
  }

  // CLAIMS ADDED HERE SHOULD MATCH WITH CLAIMS OFFERED BY METADATA
  var claims = new List<Claim>
  {
    ... // Your other claims go here
    new Claim( ClaimTypes.IsPersistent, rememberMe.ToString() ),
    new Claim( ClaimTypes.Expiration, TimeSpan.FromDays( DEFAULT_COOKIE_EXPIRATION_IN_DAYS ).ToString() )
  };

  var identity = new ClaimsIdentity( claims );

  return ClaimsPrincipal.CreateFromIdentity( identity );
}

The two steps above ensure that the STS will communicate the necessary information to the Relying Party for them to set up their session to mirror the STS session.

Relying Party setup

On the Relying Party side we have to override the default WIF behavior for the session expiration and set it manually based on the claims we’ve specified in the STS. We’ll need to override the SessionSecurityTokenCreated behavior to do so. Place the following code in the global.asax of the Relying Party.

// This method does not appear to be used, but it is.
// WIF detects it is defined here and calls it.
// Note: Do not rename this method. The name must exactly match or it will not work.
[System.Diagnostics.CodeAnalysis.SuppressMessage( "Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode" )]
void WSFederationAuthenticationModule_SessionSecurityTokenCreated( object sender, SessionSecurityTokenCreatedEventArgs e )
{
  bool isPersistent = false;
  string expirationAsString = null;
  try
  {
    isPersistent = ClaimsHelper.GetClaimValueByTypeFromPrincipal<bool>( e.SessionToken.ClaimsPrincipal, ClaimTypes.IsPersistent );
    expirationAsString = ClaimsHelper.GetClaimValueByTypeFromPrincipal<string>( e.SessionToken.ClaimsPrincipal, ClaimTypes.Expiration );
  }
  catch ( ClaimParsingException )
  {
    Trace.TraceWarning( "Failure to parse claim values for ClaimTypes.IsPersistent and ClaimTypes.Expiration. Using session cookie as a fallback." );
  }
  catch ( ClaimNullException )
  {
    Trace.TraceWarning( "Expected claim values for ClaimTypes.IsPersistent and ClaimTypes.Expiration but got null. Using session cookie as a fallback." );
  }

  TimeSpan expiration;
  if ( isPersistent && TimeSpan.TryParse( expirationAsString, CultureInfo.InvariantCulture, out expiration ) )
  {
    DateTime now = DateTime.UtcNow;
    e.SessionToken = new SessionSecurityToken( e.SessionToken.ClaimsPrincipal, e.SessionToken.Context, now, now.Add( expiration ) )
    {
      IsPersistent = true
    };
  }
  else
  {
    e.SessionToken = new SessionSecurityToken( e.SessionToken.ClaimsPrincipal, e.SessionToken.Context )
    {
      IsPersistent = false
    };
  }
  e.WriteSessionCookie = true;
}

The important part is at the end. We create a new SessionSecurityToken object based on the values of the claims and overwrite the default WIF security token with it. This gives us either a session cookie or a cookie with an expiration that matches the STS value; giving us the ‘Keep me logged in’ behavior we wanted.

--

Update Web.Config in an Azure Cloud Service package

09 August 2013

Windows Azure deployments are done using a convenient .cspkg and .cscfg files. The .cscfg is an xml config file and the .cspkg is essentially a zip file that contains your application code. This means you can build once and deploy to different environments by providing a different version of the .cscfg, making continuous deployment simple. Just keep the .cspkg file around and deploy it anywhere.

Problems arise when you need to modify something in the cspkg, such as the Web.Config for your web application. A common scenario where this is necessary is configuring Windows Identity Foundation to update a trusted issuer thumbprint or federation realm. Options to fix the problem are to create a new package for each environment or creating the package on demand as part of the deploy process. Microsoft has provided a way to create packages manually but it’s complicated to set up and involves duplicating a lot of work that’s done for us already in the MSBuild tasks for the cloud project.

The fix

An alternate approach I’ve had success with is to modify the Web.Config on role start in your web project based on values stored in the .cscfg configuration file. To do this, copy the Web.Config in your project and rename it to Web.Config_pretransform or something similar. Also stop tracking the Web.Config in your source control since it will just be generated as needed (but make sure the project still has a reference to it). Next add code to your WebRole.cs to do the file modification like so:

public override bool OnStart()
{
   UpdateConfigs();
}

Fill in the UpdateConfigs method with code to open the Web.Config_pretransform using Microsoft.Web.Administration.ServerManager.

private void UpdateConfigs()
{
  using ( var server = new ServerManager() )
  {
    Site site = server.Sites[RoleEnvironment.CurrentRoleInstance.Id + "_Web"];
    string physicalPath = site.Applications["/"].VirtualDirectories["/"].PhysicalPath;
    string inputWebConfigPath = Path.Combine( physicalPath, "web.config_pretransform" );
    string outputWebConfigPath = Path.Combine( physicalPath, "web.config" );

    File.Copy( inputWebConfigPath, outputWebConfigPath, overwrite:true );

    SetWIFWebConfigSettings( outputWebConfigPath );
  }
}

The code above grabs the physical path of the website from IIS and passes it off to the SetWIFWebConfigSettings method. This method can then parse and update the Web.Config using your favorite XML parser. Finally, the code below shows how to update the realm and requireHttps attributes using values from the .cscfg:

private static void SetWIFWebConfigSettings( string webConfigPath )
{
  var doc = XDocument.Load( webConfigPath );
  var wifConfig = doc.Descendants( "microsoft.identityModel" ).Single();

  var wsFederation = wifConfig.Descendants( "federatedAuthentication" ).Single()
    .Descendants( "wsFederation" ).Single();
  wsFederation.SetAttributeValue( "realm", CloudConfigurationManager.GetSetting( "WifWsFederationRealm" ) );
  wsFederation.SetAttributeValue( "requireHttps", "true" );

  doc.Save( webConfigPath );
}
  • It’s important to note that Azure will not actually put our instance on the load balancer until after the RoleStart method has finished which allows us to do these modifications.

One last thing we need to do is make it work locally as well. An easy fix is to copy the Web.Config.pretransform to the Web.Config location prior to building the project if it doesn’t exist.

<PropertyGroup>
   <PreBuildEvent>if not exist "$(ProjectDir)\Web.config" (copy /Y "$(ProjectDir)\Web.config_pretransform" "$(ProjectDir)\Web.config") else (echo web.config already exists in $(ProjectDir), skipping)</PreBuildEvent>
</PropertyGroup>

That’s all we need to do to modify the Web.Config on role start in an Azure Cloud Service. It lets us keep all environment specific settings in the .cscfg which means we can deploy one package to any environment.

--

Emulate The Visual Studio Command Prompt In PowerShell

06 June 2013

The Visual Studio Command Prompt provided when you install Visual Studio adds lots of useful commands to the PATH of the current prompt. You don’t have to remember where msbuild.exe or mstest.exe are located just call the commands. However, Visual Studio only ships with a regular command prompt. There is no corresponding PowerShell command prompt.

All hope is not lost for PowerShell fans though. The Visual Studio Command Prompt works by calling the vcvarsall.bat file that ships with Visual Studio to update the PATH for the current session. We can take advantage of that same script when PowerShell starts to update the PATH for the PowerShell session by modifying the default PowerShell profile.

First we need to open the PowerShell profile file for editing. This file is located at ~\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 If you already have PowerShell open you can call start $PROFILE from the prompt. This command will open the profile for you in your default editor.

Next paste these lines at the end of the file.

# Move to the directory where vcvarsall.bat is stored
pushd 'C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC'

# Call the .bat file to set the variables in a temporary cmd session and use 'set' to read out all session variables and pipe them into a foreach to iterate over each variable
cmd /c "vcvarsall.bat&set" | foreach {
  # if the line is a session variable
  if( $_ -match "=" )
  {
    $pair = $_.split("=");

    # Set the environment variable for the current PowerShell session
    Set-Item -Force -Path "ENV:\$($pair[0])" -Value "$($pair[1])"
  }
}

# Move back to wherever the prompt was previously
popd

Save the file, and reopen a new PowerShell prompt. You should have a fully functioning Visual Studio Powershell Prompt.

Finally, if you don’t want to load the Visual Studio variables for every prompt, move the lines above to a file in your path and execute that file when you want the variables . thefilethathasthescriptabove.ps1.


  • Edit 6/7/2013 - Corrected typo in Set-Item line.

--


See All Posts