Nu har jeg lavet min custom class:
using System;
using System.Security.Principal;
using Roles;
namespace CalendarMan
{
/// <summary>
/// CustomPrincipal.
/// This class i devloped because of ASP.NET's
/// Session object is loosely typed, and it's annoying to
/// typecast(boxing) it every time we need a user object
/// </summary>
/// <see>
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/SecNetHT06.asp</see> public class CustomPrincipal : IPrincipal
{
private IIdentity _identity;
private string [] _roles;
//This is the custom user object according to the User classes
private Roles.RoleDecorator user;
/// <summary>
/// Constructor
/// </summary>
public CustomPrincipal(IIdentity identity, string [] roles, Roles.RoleDecorator us)
{
_identity = identity;
_roles = new string[roles.Length];
roles.CopyTo(_roles, 0);
Array.Sort(_roles);
user = us;
}
// IPrincipal Implementation
/// <summary>
/// IsInRole, function to see if a user has a surden role
/// </summary>
/// <param name="role">the role to check</param>
/// <returns>bool</returns>
public bool IsInRole(string role)
{
return Array.BinarySearch( _roles, role ) >=0 ? true : false;
}
/// <summary>
/// Proberty Identity
/// </summary>
/// <returns>Identity</returns>
public IIdentity Identity
{
get
{
return _identity;
}
}
/// <summary>
/// Checks whether a principal is in all of the specified set of
/// </summary>
/// <param name="roles">the roles to check</param>
/// <returns>bool</returns>
public bool IsInAllRoles( params string [] roles )
{
foreach (string searchrole in roles )
{
if (Array.BinarySearch(_roles, searchrole) < 0 )
return false;
}
return true;
}
/// <summary>
/// Checks whether a principal is in any of the specified set of
/// </summary>
/// <param name="roles">the roles to check</param>
/// <returns>bool</returns>
public bool IsInAnyRoles( params string [] roles )
{
foreach (string searchrole in roles )
{
if (Array.BinarySearch(_roles, searchrole ) > 0 )
{
return true;
}
}
return false;
}
}
}