Using system.me; using system.Web.ui; using system.web.security; using system.security.principal; using aspnetforums.components;
Namespace aspnetforumus {
// ******************************************************** ********************* // userroles ///// /// The user roles class is buy to management user to role mappings. // / summary> // ************************************************************* ************************************* / Public Class Userroles {const string rolescookie = "aspnetforumsroles";
// ******************************************************** ********************* // getUserroles ///// ///connects to the user role's datasource, retrieves all the roles a given // / user belongs to, and add the the curret iprincipal. The roles are retrieved /// from the datasource or from an encrypted cookie. /// summary> // ************** *********************************************************** ********* / public void getUserroles () {httpContext context = httpContext.current; string [] userroles = null; string formatteduserroles;
// is the request authenticated? If (! Context.request.isauthentic) return;
// Get the Roles this user is in est (Context.Request.cookies [Rolescookie] == Null) || (Context.Request.cookies [Rolescookie] .value == "")) {formattedUserRoles = String.Join ";", Userroles.getuserroles (context.user.Identity.Name);
// Create authentication ticket FormsAuthenticationTicket ticket = new FormsAuthenticationTicket (1, // version Context.User.Identity.Name, // user name DateTime.Now, // issue time DateTime.Now.AddHours (1), // expires every hour False, // Don't Persist Cookie FormattedUserroles // Roles); // Encrypt The Ticket String Cookiestr = FormsAuthentication.encrypt (Ticket);
// Send the cookie to the client Context.Response.Cookies [rolesCookie] .Value = cookieStr; Context.Response.Cookies [rolesCookie] .Path = Globals.ApplicationVRoot; Context.Response.Cookies [rolesCookie] .Expires = DateTime.Now .Addminutes (5); else {// Get Roles from Roles cookie formsauthenticationticTicket Ticket = formsauthentication.Decrypt (Context.Request.cookies [Rolescookie] .value);
// convert the string representation of the role data into a string array ArrayList rolesArrayList = new ArrayList (); foreach (String role in ticket.UserData.Split (new char [] { ';'})) {if (role.Length > 0) RolesArrayList.Add (Role);
Userroles = (String []) ROLESARRAYLIST.TOARRAY (TypeOf (String));}
// Add Our OWN Custom Principal To the Request Containing The Roles in The Auth Ticket Context.user = New GeneriPrincipal (Context.user.Identity, Userroles);
// ******************************************************** ******************** // deleterole //// /// deletes a security role and any associated forum and user connections /// < / Summary> // ************************************************************* ************************************ / public static void deleteRole (String role) {iDataProviderbase dp = dataProvider.instance (); dp.deleterole (role );} // ************************************************************* ************************ // updaterole ///// /// Updates the description for a given role. /// < / Summary> // ************************************************************* *********************************** / Public Static Void Updaterole (String Role, String Description) {iDataProviderBase DP = DataProvider.instance (); DP. Updateroledescription (role, description);}
// ******************************************************** ******************** // getroledescription // /// /// Get the description of a given role /// summary> / / ************************************************** ***************************** / PUBLIC STATIC STRING GETROLEDESCRIPTION (String Role) {iDataProviderBase DP = DataProvider.instance (); return dp.getroleDescription (role);}
// ******************************************************** ******************** // CreateNewrole // //// /// Creates a new security role /// summary> // * *********************************************************** ***************************** / Public Static Void CreateNewRole (String Role, String Description) {iDataProviderBase DP = DataProvider.instance (); DP.CREATENEWROLE (Role, Description) } // ************************************************************* ************************* ADDUSERTOROL ///// /// add a specified user to a role /// summary > // ******************************************************** ********************************* / PUBLIC Static Void AdduSerTorole (String UserName, String Role) {iDataProviderBase DP = DataProvider.instance (); DP.Addusertorole Username, role);
// ******************************************************** ********************* / / AddforumTorole // /// /// adds a specified user to a role /// summary> / / ************************************************** ******************************** / Public Static Void AddforumTorole (int forumid, string role) {iDataProviderBase dp = dataProvider.instance (); DP.AddforumTance (ForumID, Role);
// ******************************************************** ********************* // RemoveUserFromRole ///// /// Removes the specified user from a role /// summary> / / ************************************************** ******************************* / Public Static Void RemoveUserFromrole (String Username, String Role) {iDataProviderBase DP = DataProvider.instance (); dp.removeuserfromrole (username, Role);} // ************************************************************ ********************************** // RemoveForumFromRole // /// /// Removes The Specified Forum from a role /// < / Summary> // ************************************************************* *********************************** / Public Static Void RemoveForumFromrole (int forumid, string role) {iDATAPROVIDERBASE DP = DataProvider.instance (); DP. Removeforumfromrole (forumid, role);}
// ******************************************************** ********************* // getllroles // // /// all the roles what the system supports /// summary> / // string array of roles returns> // *************************************************** ******************************************************** / public static string [] getAllRoles () {// create instance Of the iDataProviderBase IdataProviderBase DP = DataProvider.instance ();
Return DP.GETAllRoles ();
// ******************************************************** ******************** // getforumroles ///// /// Get All of the Roles That a given forum belongs to /// < / Summary> /// string array of roles returns> // ******************************************* ******************************************************************************************** / Public Static String [] getforumroles (int forumid) ) {// Create Instance of the iDataProviderBase iDataProviderBase DP = DataProvider.instance (); returnid;}
// ******************************************************** ******************** // getUserroles // /// /// all the roles that the named user belongs to /// summary > /// Name of user to retrieve roles for param> /// string array of roles returns> // ************** *********************************************************** ********** / public static string [] getUserroles (String username) {// create instance of the iDataProviderBase iDataProviderBase DP = dataProvider.instance ();
Return dp.getuserroles (username);
// ******************************************************** ******************** // sIGNOUT / / / / / /// cleans up cookies use for role management when the user signs out. // / summary> // ************************************************************* *********************************************** / public static void sign () {httpcontext context = httpContext.current;