Looking at the download numbers, my add-on library for System.IdentityModel was quite popular. Some days ago I started looking the code to see what could be still useful in the face of Geneva.
The good news is, that a lot of my helper classes are not necessary anymore thanks to the easier claims model in Geneva. Still I think that most of the time you have to wrap the raw IClaimsPrincipal with more domain specific functionality. Since my current project makes heavy use of Geneva I began compiling a set of classes that made my life easier while working with the Geneva framework.
Like in the first release, I added a bunch of extensions methods that make finding and demanding claims easier. This is mostly syntactic sugar because the new model is very LINQ friendly and you can easily write the queries yourself – but it makes the code easier to read IMO. At the heart of all functionality is this extension method:
public static IEnumerable<Claim> FindClaims(
this IClaimsIdentity identity, Predicate<Claim> predicate)
return from claim in identity.Claims
This allows layering all kinds of higher level functionality on top of both IClaimsIdentity and IClaimsPrincipal, e.g:
- FindClaims(commonly used parameters)
Returns a (possibly empty) list of claims.
- DemandClaim(commonly user parameters)
Throws a SecurityException if a specified claim is not found
- GetClaimValue / TryGetClaimValue
Returns the value of a specified claim
..and on top of that it is very easy to add business problem centric functionality.
This allows me to write code like this:
var principal = Thread.CurrentPrincipal.AsClaimsPrincipal();
I will release the code once I have done more testing.