If you have worked some with Windows Identity Foundation you probably know that most of the types that you work with are found in the Microsoft.IdentityModel.* namespaces. You have probably also noticed that more than a few of the type names in there are also used in corresponding locations in the System.IdentityModel.* namespaces of the base class library. Examples of such type names are Claim (in Microsoft.IdentityModel.Claims and System.IdentityModel.Claims) and RequestSecurityToken (in Microsoft.IdentityModel.Tokens and System.IdentityModel.Tokens). Of course, these types are not replaceable with each other, so you need to make sure to reference the correct namespace when dealing with them. And that would not be so tricky if it was not for the fact that many of them also share some dependencies.

For instance, let’s say you are working with Saml2SecurityToken (found in Microsoft.IdentityModel.Tokens.Saml2), it is derived from SecurityToken (found in System.IdentityModel.Tokens). In the same method, you need to use a SecurityTokenDescriptor. Working with Windows Identity Foundation, more often than not you will want to use the counterpart found in the Microsoft.IdentityModel.* namespaces rather than those found in System.IdentityModel.*. To resolve this you will need to use namespace aliases. I have found that when using ReSharper to resolve this, it tends to import the System.IndentityModel.* namespaces as usual, and set up aliases for the collisions in Microsoft.IdentityModel.*:

using System.IdentityModel.Tokens;
using SecurityTokenDescriptor = Microsoft.IdentityModel.Tokens.SecurityTokenDescriptor;

I have also found that on a regular basis this is not very practical. For instance, quite a few of the types that you work with need to have aliases set up for them. In these cases I find it more practical to start off with manually setting up aliases for the few classes from the System.IdentityModel.* that I want to use, and then have ReSharper resolve the others. Typically this results in a smaller number of regular using directives.

As an example, in one of my code files the following using directives:

using System.IdentityModel.Tokens;
using Microsoft.IdentityModel.Claims;
using Saml2Attribute = Microsoft.IdentityModel.Tokens.Saml2.Saml2Attribute;
using Saml2AttributeStatement = Microsoft.IdentityModel.Tokens.Saml2.Saml2AttributeStatement;
using Saml2SecurityToken = Microsoft.IdentityModel.Tokens.Saml2.Saml2SecurityToken;
using SecurityTokenDescriptor = Microsoft.IdentityModel.Tokens.SecurityTokenDescriptor;

...could be narrowed down to this:

using Microsoft.IdentityModel.Claims;
using Microsoft.IdentityModel.Tokens;
using Microsoft.IdentityModel.Tokens.Saml2;
using SecurityToken = System.IdentityModel.Tokens.SecurityToken;

OK, perhaps not a huge difference, but that last one looks a lot less like a workaround than the first one, to me at least.

kick it on DotNetKicks.com

I am working for a great consultancy company called Diversify, located in Sweden. We are hiring skilled .NET developers. If you are interested, don't hesitate to get in touch with me.


April 3. 2012 13:11


WIF and Colliding Namespaces

You've been kicked (a good thing) - Trackback from DotNetKicks.com