I’m implementing claims based security in my .net 4.5 application. Lots of hoops to jump through, but it is basically working.
The only part I don’t like is that I can’t create my own attributes. ClaimsPrincipalPermissionAttribute is sealed. Why?
I’m always marking throughout my application such as:
[ClaimsPrincipalPermission(SecurityAction.Demand, Resource = "Foo", Operation = "Bar")]
And since I want my resource and operation strings to not get misspelled and be easily refactorable, I have created classes so I can do this:
[ClaimsPrincipalPermission(SecurityAction.Demand, Resource = Resources.Foo, Operation = Operations.Foo.Bar)]
(Note that since different resources may have different operations, the operations themselves are subclassed by resource.)
This all works fine and dandy, but it’s a hell of a lot to type or copy/paste every time. I’d rather do something like:
[DemandPermission(Resources.Foo, Operations.Foo.Bar)]
I could create this attribute, but I would need to inherit from ClaimsPrincipalPermissionAttribute, which I can’t because it’s sealed. 🙁
Is there some other way to approach this? Perhaps I don’t need to inherit, but can I register my own attribute type somehow so it works in all the same places?
Eric Lippert talked about the commonness of sealed in Framework types, and since we are talking about code security, this bit is very important:
This is even more important in this case,
ClaimsPrincipalPermissionAttributeis checked viaIClaimsPrincipalan interface. So by makingClaimsPrincipalPermissionAttributesealed, they allow any implementer ofIClaimsPrincipalto not have to worry about hostile implementations. This is quite a savings, given this is all security related.