[Transfer] Implement a role mechanism in the forum of ASP.NET

zhaozj2021-02-16  63

Construction class:

Using system;

Using system.collections;

Using system.security.principal;

Namespace wuyin.forums.components

{

Internal Sealed

Class forumsprincipal: iPrincipal

{

PRIVATE IIDENTINTITY;

Private

String [] _roles;

Public forumSprincipal (iidentity Identity,

String [] roles)

{

_IDENTITY

= Identity;

_Roles

=

New

String [Roles.Length];

Roles.copyto (_roles, 0);

Array.Sort (_Roles);

}

// iprincipal importation

public

Bool isinrole

String role)

{

Return Array.BinarySearch (_Roles, Role)> 0?

True:

False;

}

Public iIdentity Identity

{

get

{

Return_Identity;

}

}

// Checks WHETHER a Principal is in all of the specified set of roles

public

Bool isinalllroles (Params

String [] roles)

{

FOREACH

String SearchRole

in roles)

{

IF (array.binarysearch (_roles, searchrole) <0)

Return

False;

}

Return

True;

}

// Checks WHETHER a Principal is in any of the specified set of roles

public

Bool isinanyroles (params

String [] roles)

{

FOREACH

String SearchRole

in roles)

{

IF (array.binarysearch (_roles, searchrole)> 0)

Return

True;

}

Return

False;

}

}

}

UserRole class:

Using system;

Using system.Web;

Using system.web.security;

Namespace wuyin.forums

{

///

Summary description of /// userroles.

///

public

Class userroles

{

Public userroles ()

{

//

// TODO: Add constructor logic here

//

String cookiename

= FormSauthentication.formiename;

HTTPCOOKIE Authcookie

= System.Web.httpContext.current.request.cookies [cookiename];

IF

null == Authcookie)

{

// there is no authentication cookie.

Return;

}

FormsAuthenticationalTicket Authticket

=

NULL;

Try

{

Authticket

= Formsauthentication.decrypt (authcookie.value);

Catch

{

// log exception details (OMITIted for simplicity)

Return;

}

IF

null == authticket)

{

// cookie failed to decrypt.

Return;

}

// when Ticket Was Created, The UserData Property Was Assigned A

// pipe delimited string of role names.

String [] ROLES

= Authticket.UserData.split ('$');

// Create An Identity Object FormSidentity ID

=

New Formsidentity (Authticket);

// this Principal Will Flow Throughout The Request. Wuyin.forums.components.forumSprincipal Principal

=

New Wuyin.forums.comPonents.forumSprincipal (ID, ROLES);

// attach the new principal object to the capital httpContext object system.Web.httpContext.current.user

= Principal;

}

public

Static

Void AddforuMtorole

Int forumID,

String role)

{

}

public

Static

Void AdduSerTorole

String username,

String role)

{

}

public

Static

Void Createnewrole

String role,

String description)

{

}

public

Static

Void deleterole

String role)

{

}

public

Static

String [] getallroles ()

{

//Wuyin.forums.data.sqldataprovider wuyin.forums.data.dataProvider.instance (). = New wuyin.forums.data.sqldataProvider ();

String [] s

= Wuyin.forums.data.dataProvider.instance (). GetAllRoles ();

// wuyin.forums.data.dataProvider.instance (). Dispose ();

Return S;

}

public

Static

String [] getforumroles

int forumid)

{

//Wuyin.forums.data.sqldataprovider wuyin.forums.data.dataProvider.instance (). = New wuyin.forums.data.sqldataProvider ();

String [] s

= Wuyin.forums.data.dataProvider.Instance (). Getforumroles (forumID);

//Wuyin.forums.data.dataProvider.Instance (). Dispose ();

Return S;

}

public

Static

String getroledescription

String role)

{

//Wuyin.forums.data.sqldataprovider wuyin.forums.data.dataProvider.instance (). = New wuyin.forums.data.sqldataProvider ();

String s

= Wuyin.forums.data.dataProvider.instance (). Getroledescription (role) .get ("description"). TOSTRING ();

// wuyin.forums.data.dataProvider.instance (). Dispose ();

Return S;

}

public

Void getUserroles ()

{

String roles = "";

String [] Userroles

= Userroles.getallroles ();

FOR

INT i = 0; I

{

Roles = Userroles [i];

IF (i

Roles = "$";

}

//this.controls.add(new system.web.ui.literalcontrol (roles));

// Return;

// Create the Authentication Ticket FormsauthenticationTicket Authticket

=

New

FormsauthenticationalTicket (1,

Null, DateTime.now, DateTime.maxValue,

True, Roles);

// User Data

// Now Encrypt the Ticket.

String EncryptedTicket

= Formsauthentication.encrypt (authticket);

// Create a cookie and add the encrypted ticket to the

// cookie as data. Httpcookie rolescookie

=

New httpcookie ("wuyinforumsroles", encryptedticket;

System.Web.httpContext.current.response.cookies.add (rolescookie);

}

public

Static

String [] GetUserRoles

String Username)

{

//Wuyin.forums.data.sqldataprovider wuyin.forums.data.dataProvider.instance (). = New wuyin.forums.data.sqldataProvider ();

String [] s

= Wuyin.forums.data.dataProvider.Instance (). GetUserroles (username);

//Wuyin.forums.data.dataProvider.Instance (). Dispose ();

Return S;

}

public

Static

Void RemoveforumFromrole

Int forumID,

String role)

{

}

public

Static

Void RemoveUserFromrole

String username,

String role)

{

}

public

Static

void signout ()

{

FormsAuthentication.signout ();

}

public

Static

Void Updaterole

String role,

String description)

{

}

}

}

In Global.asax: