Search EggHeadCafe's Job Board
EggHeadCafe Silverlight WPF ASP.NET VB.NET C# Excel SQL Server SharePoint
search
.NET Framework GroupsView
.NET Distributed_Apps
.NET
.NET ADO.NET
.NET ASP.NET
.NET ASP.NET Security
.NET ASP.NET Webcontrols
.NET ASP.NET Web Services
.NET Clr
.NET Compact Framework
.NET Drawing
.NET Interop
.NET Performance
.NET Web Services
.NET Windows Forms
.NET Windows Forms Controls
.NET General
.NET Csharp
.NET Visual Basic
.NET Vc
.NET Security
.NET Xml
Vsnet Debugging
Xml
Xsl
Scripting Jscript
Scripting Visual Basicscript
Scripting Wsh
Smartphone Developer
Visual Basic Com
Visual Basic Controls
Visual Basic Crystal
Visual Basic Database Ado
Visual Basic Syntax
Visual Basic Winapi
Vc Atl
Vc Debugger
Vc Language
Vc Mfc
Vc Stl
Visio Developer Visual Basica
Windowsce Embedded Vc
Windows Powershell
Visual Basic Vista Compatibility
Deployment Server
.NET Micro Porting

Group SummariesView
.NET Framework
Access
BizTalk
Certifications
CRM
DDK
Exchange Server
FoxPro
French
French .NET
Games
German
German .NET
Graphic Design
IIS
Internet
ISA Server
Italian
Italian .NET
Maps
MCIS
Miscellaneous
Mobile Apps
Money
MSN
Networking
Office
Ops Mgr
Publisher
Security
SharePoint
Small Business
Spanish
Spanish .NET
SQL Server
Systems Management Server
Transaction Server
Virtual PC / Virtual Server
Visual Studio
Win32
Windows 2000
Windows 2003 Server
Windows 7
Windows Live
Windows Media
Windows Update
Windows Vista
Windows XP
 

View All Microsoft NET Csharp Posts  Ask A New Question 

Custom IPrincipal with a Generic

sloan posted on Tuesday, June 26, 2007 12:13 PM

I'm working on a custom IPrincipal.

Sometimes I use the "good ole" MS system of strings.

Lately, I've been storing my roles and rights as Guids.

Check out the below code.

I'm not sure if this is the way to go or not.

What stinks is that I don't see how to keep IRolesAndRightsPrincipalT<T> to
be ~only string or guid based.  ( The "where" seems to only like base class
stuff  for trying to constrain <T>)

Aka, somebody could right a

public interface IRolesAndRightsPrincipalABC :
IRolesAndRightsPrincipalT<Employee>
{

}

which really doesn't make any sense.



This is a very , very "top level" interface I'm trying to create, so that it
fills IPrincipal needs throughout the company.
There might be 20-25 concrete implementations.  ( << Don't ask )



I don't know.  I'm just looking for some "This is dumb" or "That's ok"
comments.



public interface IRolesAndRightsPrincipalT<T> :
System.Security.Principal.IPrincipal

{

bool IsInRole(T role);

bool IsInAnyRole(T[] roles);

bool IsInAllRoles(T[] roles);

bool HasRight(T right);

bool HasAnyRight(T[] rights);

bool HasAllRights(T[] rights);

}



public interface IRolesAndRightsPrincipalString :
IRolesAndRightsPrincipalT<string>

{

}

public interface IRolesAndRightsPrincipalGuid :
IRolesAndRightsPrincipalT<System.Guid>

{

}
reply

 

I wouldn't use generics here.

Nicholas Paldino [.NET/C# MVP] posted on Tuesday, June 26, 2007 12:45 PM

I wouldn't use generics here.  I also wouldn't extend IPrincipal in the
way that you are.

First, if I did use generics, I would do something like this instead:

public interface IRolesAndRightsPrincipal :
System.Security.Principal.IPrincipal
{
bool IsInRole<T>(T role);
bool IsInAnyRole<T>(T[] roles);
bool IsInAllRoles<T>(T[] roles);
bool HasRight<T>(T right);
bool HasAnyRight<T>(T[] rights);
bool HasAllRights<T>(T[] rights);
}

The reason for this is that you might want to use strings or Guids on
any of the methods.

The constraint system fails you here, in that you can't say "allow this
or this".  If you have the type parameter on the type level, you could at
least create a static constructor to perform a run-time check to see if the
type parameters are valid (it's not preferred, but it's the only way right
now).  You don't have any such thing for method type parameters.

However, in this case, using strings seems to me to be nothing more than
a utility method to prevent the user from having to perform conversions to a
Guid.  If this is the case, then this isn't something that you need to
provide a custom interface implementation for.  You can just create a
utility wrapper which takes strings and performs the conversions, providing
overloaded methods which take string parameters.  This would change your
interface to this:

public interface IRolesAndRightsPrincipal :
System.Security.Principal.IPrincipal
{
bool IsInRole(Guid role);
bool IsInAnyRole(Guid[] roles);
bool IsInAllRoles(Guid [] roles);
bool HasRight(Guid right);
bool HasAnyRight(Guid[] rights);
bool HasAllRights(Guid[] rights);
}

Your utility class would look like this:

public class RolesAndRightsPrincipalUtils : IRolesAndRightsPrincipal
{
// The implementation to defer to.
private IRolesAndRightsPrincipal implementation = null;

public RolesAndRightsPrincipalUtils(IRolesAndRightsPrincipal
implementation)
{
// Store the implementation.
this.implementation = implementation;
}

public bool IsInRole(Guid role)
{
// Forward the call.
return implementation.IsInRole(role);
}

public bool IsInRole(string role)
{
// Convert to a guid, and call the overload.
return implementation.IsInRole(new Guid(role));
}

// And so on...
}

This would keep your design clean, assuming everything was represented
with Guids (roles and rights).

Now, I am assuming that you are actually doing some work on the
combination of guids and rights to come up with a string which you are
passing to the IPrincipal implementation of IsInRole to determine if the
user has the appropriate rights.  If this is the case, then you really don't
need the IRolesAndRightsPrincipal interface, as you can just have the logic
in the RolesAndRightsPrincipalUtils class, performing the transformation and
then calling IsInRole on the IPrincipal implementation.


--
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com
}
reply

Hmm.

sloan posted on Tuesday, June 26, 2007 1:31 PM

Hmm.

I think your RolesAndRightsPrincipalUtils
(aka, the Adapter Design Pattern I think)
seems like a better solution.

Ok, thanks.

But sometimes I am using roles / rights in a string format.

string ROLE_1 = "Admin";
string ROLE_2 = "NormalUser";
string ROLE_3 = "Guest";

I'm going to think about it some more.

But I think we (me and the mouse in my pocket) are going to mostly guid
solution.

Thanks Nicholas.




the
this
the
than
a
providing
don't
logic
and
that
reply


Previous Microsoft NET Csharp conversation.