4
votes

I'm building my own token based authentication for asp.net web api 2 and I have a question about the user class.

Currently my user class implements IPrincipal and IIdentity, but I don't know what is the best practice. Would you divide IPrincipal and IIdentity and make the IIdentity implementation a member of IPrincipal?

Currently I use the following class declaration

class User : IPrincipal, IIdentity

Thank you!

1
A class Implements an interface - it doesn't inherit from it ... any class can only ever inherit from a single base class (but it can implement multiple interfaces)marc_s
Why not? These are interfaces, they are meant to be implemented. Is your question about implementing both interfaces in the same class?Sergey Kalinichenko

1 Answers

3
votes

Would you divide IPrincipal and IIdentity and make the IIdentity implementation a member of IPrincipal?

I don't think this is a good approach, since if the Identity was similar to IPrincipal there shouldn't be any reason for being separated.

Furthermore, as of now your class implements both interfaces. That means your class implements the methods of both interfaces. If you decide later for any reason that your class shouldn't implement one or another interface, you should just have to remove the implementation of the methods that are tied to the interface you want to remove.

In addition to the above keep in mind the following:

The interface-segregation principle (ISP) states that no client should be forced to depend on methods it does not use

This is one of the five SOLID principles, which are some basic principles of object oriented programming and design.