Re: Interface question
- From: "Patrick.O.Ige" <naijacoder@xxxxxxxxxxx>
- Date: Tue, 25 Apr 2006 15:41:38 +1000
David very good links there
Patrick
"David Hogue" <davehogue+news@xxxxxxxxx> wrote in message
news:z3g3g.101597$zy2.5267@xxxxxxxxxxxxxxxxxxxxxxxxx
tshad wrote:
"David Hogue" <davehogue+news@xxxxxxxxx> wrote in messageWe can (and did) use GenericPrincipal to handle our own users and roles.
news:NoS2g.168323$iS4.4815@xxxxxxxxxxxxxxxxxxxxxxxxx
tshad wrote:
"David Hogue" <davehogue+news@xxxxxxxxx> wrote in messageMy understanding of IPrincipal and IIdentity isn't the best, but I
news:uBH2g.79071$7i1.2213@xxxxxxxxxxxxxxxxxxxxxxxxx
Implementing the IPrinciple interface won't have any affect on howIs that the main reason? Other than setting up security, why would I
your
CustomPrincipal class behaves.
The difference is if you have a function that requires an argument of
type IPrincipal (sorry I don't have an example handy). You would be
able to pass an instance of your first CustomPrincipal in to that
function, but the second would generate a compiler error since it's
not
the right type.
need
IPrincipal and IIdentity?
believe they are used primarily for security. Setting user roles and
that kind of thing.
I am not trying to NOT use it. I am just trying to find out why it isIt's only better if you need treat the class as an IPrincipal.
better than the identical code, without the IPrincipal inheritance.
I assume that the windows Authorization, such as Forms, doesn't needWindows and Forms based authentication both use the interfaces, but you
it.
I
mean does windows (IIS) look for IPrincipal and IIdentity when it is
using
managed security context?
don't need to interact with them directly unless you want to change
default behavior.
For example: The company I work for has a system with users and roles
stored in a database. When a request is authenticated we get a list of
roles and create a GenericPrincipal (GenericPrincipal implements
IPrincipal):
That is what I am curious about.
When you talk about users and roles stored in a database, are you talking
about Windows Users and Roles (User Policies)?
I assume you can't use the GenericPrincipal to handle your own users and
roles, such as office managers and their roles. These would be stored in
your own database and the GenericPrincipal wouldn't know about that.
This is because it is a "generic", for lack of a better term,
implementation of the IPrincipal interface. We can give a
GenericPrincipal an array of strings and it will return true from the
IsInRoles method if that role is in the array.
We did't have to create our own class that implemented IPrincipal,
GenericPrincipal was already there for us. However the downside is that
the code to get the users from the database still has to go somewhere
and it ended up mixed in with a bunch of unrelated code.
Interfaces and some of the more abstract object oriented ideas can bestring[] userRoles = db.GetUserRoles(Context.User.Identity.Name);
Context.User = new GenericPrincipal(Context.User.Identity, userRoles);
Though we didn't use a custom principal class we could have and it might
look something like this:
If you didn't use a custom principal class, I assume you don't store your
own users, roles a policies, right?
public class SmartzPrincipal : IPrincipal
{
private IIdentity identity;
public SmartzPrincipal(IIdentity identity)
{
this.identity = identity;
}
public IIdentity Identity
{
get { return identity; }
}
public IsInRoles(String role)
{
List<string> roles = db.GetUserRoles(identity.Name);
return roles.Contains(role);
}
}
Then we would use it like so:
Context.User = new SmartzPrincipal(Context.User.Identity);
While the version with the interface looks longer in my example it would
actually be simpler than what we have. It would encapsulate all the
role based code in one place and be more reusable.
Also, is CustomPrincipal of type IPrincipal just because it inheritsImplements is the word I hear most often when referring to interfaces.
from
it
(maybe I am getting inheritance confused with interfaces).
It's really the same concept as inheriting from a base class, except you
can implement multiple interfaces and only inherit from one base class.
CustomPrincipal is of type IPrincipal because it implements it.
I am trying to see all the reasons why to use interfaces.I hope I'm helping and not just confusing things...
Thanks,
Tom
You are. I am just trying to really understand it. I sort of understand
interfaces, but am trying to find the reasons to use them vs when not to
use
them. Just because you can doesn't mean you should (in all cases).
difficult to fully understand. I've been working with object oriented
languages for years, but it's only relatively recently that my code has
become more object oriented.
I would suggest reading up on design patterns for some insight into how
interfaces are often used. There was a recent article on codebetter.com
that might be a good place to start:
http://codebetter.com/blogs/jeremy.miller/archive/2006/04/11/142665.aspx
--
David Hogue
.
- References:
- Interface question
- From: tshad
- Re: Interface question
- From: David Hogue
- Re: Interface question
- From: tshad
- Re: Interface question
- From: David Hogue
- Re: Interface question
- From: tshad
- Re: Interface question
- From: David Hogue
- Interface question
- Prev by Date: Re: validating groups of validators in 1.1
- Next by Date: dotnet 2.0: Is it possbile for the ImageMap control to render valid XHTML?
- Previous by thread: Re: Interface question
- Next by thread: Re: Interface question
- Index(es):
Relevant Pages
|