I have been engaged in a debate regarding why there are separate IPrincipal and IIdentity objects in the .NET security model. I've explained that the principal represents the security context and allows for role-checking while the identity is just that, the identity of the current user (name, etc.). However, having been confronted with numerous questions and arguments, I am trying to understand the practical side of this as we follow this practice with Csla.
In all but one case that I've been able to find, there is a one-to-one relationship between the principal and identity classes (WindowsPrincipal/WindowsIdentity, PassportPrincipal/PassportIdentity, GenericPrincipal/GenericIdentity). The only exception is the FormsIdentity object which will be contained by a GenericPrincipal object.
In researching the literature, Microsoft makes the statement that the only legitimate reasons why you would want to create your own custom principal is if you want to overload the IsInRole method (as is done with the WindowsPrincipal class) or add additional functionality (which is not demonstrated anywhere).
Given this, why then have a BusinessPrincipalBase class that does nothing different from GenericPrincipal? Couldn't the custom identity be simply wrapped in a GenericPrincipal (as with FormsIdentity) using something similar to:
IPrincipal p = new GenericPrincipal(myIdentity);
That's the first question. Second question is aside from the built-in forms authentication (using FormsIdentity), can anyone give me an example where it makes sense to have a separate identity class?
What I mean is, in the ProjectTracker sample application, there are custom PTPrincipal and PTIdentity classes. Why not just have one class that implements both interfaces if there is a one-to-one relationship?
Many more thoughts have been thrown around here but I will leave it at that and open the floor for your comments and thoughts.
Thx
Think that BuisinessPrincipalBase is serializable class and GenericPrincipal is not.
And Separation of responsibilities may prove usefull in complex scenarios.
Max
Figuruing it out a little i came to probable answer- Principal object can be used for caching some info in it that is not relevant to underlying identity.
And a little off the question - dosnt it seems to you that RolesForProperty is too granular for practical purposes, it adds heavyness to the business object?
It would be better to have RolesForObject instead, because object resposible for some functional resource that user in its entirety can read or write.
I think that such granularity is bad design of objects.
P.S.- Maybe its a question to Rocky:)
why there are separate IPrincipal and IIdentity objects in the .NET security model
The separation is just good OOP to follow SRP. IIdentity is for authentication and IPrincipal is for authorization
Given this, why then have a BusinessPrincipalBase class that does nothing different from GenericPrincipal? Couldn't the custom identity be simply wrapped in a GenericPrincipal (as with FormsIdentity) using something similar to:
IPrincipal p = new GenericPrincipal(myIdentity);
Actually it should be new GenericPrincipal(myIdentity, myRoles); This is a good question. I could not find the reason in the book and I have never use it. goracio, did you try that the GenericPrincipal could not be serialized? I took a peek using reflector and found signature of this class is as follow:
Copyright (c) Marimer LLC