Wednesday, January 18, 2017

Failure to start website in IIS Express: Process with an ID #### is not running

Last week during a training one of the participants got the following error when she tried to debug a web application in IIS Express. IIS Express failed to start and Visual Studio showed the following error message:

Process with an ID #### is not running

We couldn’t find the root cause of the issue but we had a workaround to fix it:

  • Close Visual Studio.
  • Delete the hidden .vs folder on solution level
  • Open your solution again
  • This time IIS express should be back up and running

Tuesday, January 17, 2017

ASP.NET Core: User.Identity.Name remains empty after authenticating using OIDC

After solving the problem I had yesterday, my OIDC middleware in ASP.NET Core was finally working. I was able to login and I could find all my claims inside my ClaimsIdentity.

However this was not the end of all my problems as I noticed that the User.Identity.Name value was empty. Strange! Because when I took a look at my claims, a name claim was certainly there…

image

What is going on?

The thing is that Microsoft provided a NameClaimType and also a RoleClaimType property on ClaimsIdentity. These properties define which claim should be used to represent the name(and role) claim on your User.Identity.  As a default value they decided on using the following claimtypes (which were part of WIF):

These claimtypes are not part of the OIDC claim types and this explains why no mapping is happening…

To fix this you can update your OIDC middleware by adding a TokenValidationParameters section:

Monday, January 16, 2017

ASP.NET Core: OpenIdConnectMiddleware returns 401 after oidc was challenged

The application we are working on is using OpenID Connect(OIDC) to externalize authentication. However after configuring the OIDC middleware(Microsoft.AspNetCore.Authentication.OpenIdConnect)in ASP.NET Core it failed to work.

Instead of being redirected to the Identity Provider (by returning a 302) I got a 401 and the page just remains blank Sad smile.  Inside my output logs I could see the following:

'FrontEnd.Web.exe' (CLR v4.0.30319: FrontEnd.Web.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Runtime.Serialization\v4.0_4.0.0.0__b77a5c561934e089\System.Runtime.Serialization.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.

Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectMiddleware: Information: AuthenticationScheme: oidc was challenged.

Microsoft.AspNetCore.Hosting.Internal.WebHost: Information: Request finished in 444.9692ms 401 

On GitHub I found that there are some problems with the newer versions(1.1.*) of Authentication.Cookies and OpenIdConnect nuget packages. By changing the version back to 1.0.*.  I was able to solve the issue.

Friday, January 13, 2017

ASP.NET Core MVC–Register a global authorization filter

ASP.NET Core MVC still supports the concept of action filters. On the project I’m currently working on I wanted to add a global Authorization filter.

So similar to ASP.NET MVC, I tried to add the AuthorizeAttribute as a global filter:

However this didn’t work. In ASP.NET Core MVC the Attribute class and the filter implementation are separated from each other. Instead I had to use the following code:

Thursday, January 12, 2017

Enums alternative in C#

While reading the following blog post(http://piotrgankiewicz.com/2017/01/09/async-http-api-and-service-bus/) I noticed this code snippet:

This code is taking advantage of the Expression-bodied function members in C# 6. And although this is just a static class with some readonly properties I found it aesthecially pleasing and it looks like a useful (and more flexible) alternative to enums.

If you look at the syntax you had to use before, it just feels more like a normal class instead of an enum construct…

An alternative approach would be using Getter-only auto-properties but it doesn’t feel the same as well…

Wednesday, January 11, 2017

CQRS: Building a pipeline using MediatR behaviors

I recently started a new project where we are using MediatR to simplify our CQRS infrastructure. This gives us our basic building blocks to create our queries, commands and the related handlers.

One of the things I wanted to add were some decorators to provide some extra functionality(like authorization, logging, transaction management,…) before and after a request. Turns out I’m lucky as with the release of MediatR 3.0 I can fall back to a built-in functionality called behaviors.

Similar to action filters in ASP.NET MVC, it gives me the opportunity to execute some arbitrary logic before and after a request(command/query). Usage is simple:

  • You have to implement the IPipelineBehavior<TRequest, TResponse> interface:
  • Don’t forget to register the pipeline behavior through your IoC container(StructureMap in the example below)!
Note: As I’m registering this as an Open Generic this behavior will apply to all my messages.

Tuesday, January 10, 2017

Formatting strings in F#

As F# is build on top of the CLR and has access to the same Base Class Library you can use String.Format as you know from C#.

A (better) alternative is the printf function in F#. Be aware that the printf function(and its relatives) are not using the composite formatting technique we see in String.Format but use a C-style technique instead.

This means that you don’t use positional placeholders like this:

String.Format("A string: {0}. An int: {1}. A float: {2}. A bool: {3}","hello",42,3.14,true)

Instead you use C-style format strings representing the data types(e.g. %s for string):

printfn "A string: %s. An int: %i. A float: %f. A bool: %b" "hello" 42 3.14 true
What makes this approach really nice is that it adds an extra layer of type safety on top of your string format. If you expect a string parameter but provide an integer, you’ll get a nice compiler error.
But maybe you are wondering; what about string interpolation, a nice feature added to C# 6? Unfortunately this doesn’t exist (yet) in F# although a Language Specification is available(https://github.com/fsharp/fslang-design/blob/master/RFCs/FS-1001-StringInterpolation.md)

More information: https://fsharpforfunandprofit.com/posts/printf/