Dynamic ClaimsPrincipal with C# 4

Travis wrote an interesting blog post about mixing the new dynamic feature in C# 4 and claims. His idea was to use a User class derived from DynamicObject as a proxy to get claims. Here is some of the code he shows:

var claims = new[]
        new Claim("foo", "3"),
        new Claim("foo_bar", "true"),
        new Claim("foo_baz", "Ted"),
        new Claim("http://schemas.travisspencer.com/2010/05/test/claims/shoesize", "11"),
        new Claim("http://schemas.travisspencer.com/2010/05/test/claims/haircolor", "blond"),
        new Claim("Age", "16"),
    var identity = new ClaimsIdentity(claims);
    dynamic user = new MyGoodUser(identity);
    Console.WriteLine("Foo = {0}", user.Foo);

Now, If you look carefully most of the claims are defined with a long namespace plus a friendly name, like:


So wouldn’t it be nice if I could do something like


Well, I took Travis code and tweaked here and there and here is how it looks:


This is user experience applied to the API :) I like to call it DX (Developer Experience).

I posted the code here


blog comments powered by Disqus