|
using System;
using System.Security.Principal;
namespace ASPNET.StarterKit.TimeTracker.BusinessLogicLayer
{
//*********************************************************************
//
// CustomPrincipal Class
//
// The CustomPrincipal class implements the IPrincipal interface so it
// can be used in place of the GenericPrincipal object. Requirements for
// implementing the IPrincipal interface include implementing the
// IIdentity interface and an implementation for IsInRole. The custom
// principal is attached to the current request in Global.asax in the
// Authenticate_Request event handler. The user's role is stored in the
// custom principal object in the Global_AcquireRequestState event handler.
//
//*********************************************************************
public class CustomPrincipal : IPrincipal
{
private int _userID;
private string _userRole = String.Empty;
private string _name;
// Required to implement the IPrincipal interface.
protected IIdentity _Identity;
public CustomPrincipal()
{
}
public CustomPrincipal(
IIdentity identity,
int userID,
string userRole,
string name)
{
_Identity = identity;
_userID = userID;
_userRole = userRole;
}
// IIdentity property used to retrieve the Identity object attached to
// this principal.
public IIdentity Identity
{
get { return _Identity; }
set { _Identity = value; }
}
// The user's ID, created when the user was inserted into the database
public int UserID
{
get { return _userID; }
set { _userID = value; }
}
// The user's role, as defined in TTUser.
public string UserRole
{
get { return _userRole; }
set { _userRole = value; }
}
// The user's name (either First and Last name, or their network username)
public string Name
{
get { return _name; }
set { _name = value; }
}
//*********************************************************************
//
// Checks to see if the current user is a member of AT LEAST ONE of
// the roles in the role string. Returns true if found, otherwise false.
// role is a comma-delimited list of role IDs.
//
//*********************************************************************
public bool IsInRole(string role)
{
string [] roleArray = role.Split(new char[] {','});
foreach (string r in roleArray)
{
if (_userRole == r)
return true;
}
return false;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.
A list of licenses authors might use can be found here