Learn More





Welcome to the DNN Community Forums, your preferred source of online community support for all things related to DNN.
In order to participate you must be a registered DNNizen

HomeHomeDevelopment and...Development and...Building ExtensionsBuilding ExtensionsModulesModulesProgrammatically adding User Roles Programmatically adding User Roles
New Post
8/25/2014 5:16 PM

Hi Folks,

I'm having a little trouble with programmatically adding User Roles, and being able to get a decent handle on the current User Roles after adding.

I'm able to add a Role to a User just fine, but it's not until the second page refresh that my "output" textbox has the expiryDate as I expect.

In stepping through the following code in debug; after adding the Role - my UserInfo (cUser in this case) - is not that in that role, until I refresh the page again.

Running DNN 7.3



                if (HttpContext.Current.Session["trialUser"] != null)

                    sTrialStatus = HttpContext.Current.Session["trialUser"].ToString();


                if (sTrialStatus == "Trial")


                    bTrialUser = true;




                    bTrialUser = false;


                if (Request.IsAuthenticated && !_currentUser.IsInRole("Trial") && bTrialUser == true)


                    RoleController ctrRoles = new RoleController();

                    RoleInfo oCurrentRole = ctrRoles.GetRoleByName(this.PortalId, "Trial");

                    uRole = ctrRoles.GetUserRole(this.PortalId, _currentUser.UserID, oCurrentRole.RoleID);

                    ctrRoles.AddUserRole(this.PortalId, _currentUser.UserID, oCurrentRole.RoleID, System.DateTime.Now, System.DateTime.Now.AddDays(1));




                if (!Page.IsPostBack)


                  UserInfo cUser = new UserInfo();

                    cUser = UserController.GetCurrentUserInfo();


                    if (!UserInfo.IsInRole(PortalSettings.AdministratorRoleName.ToString()) && cUser.IsInRole("Trial"))


                        DotNetNuke.Security.Roles.RoleController rolesController = new DotNetNuke.Security.Roles.RoleController();

                        UserRoleInfo role = rolesController.GetUserRole(0, _currentUser.UserID, rolesController.GetRoleByName(0, "Trial").RoleID);

                        expiryDate = role.ExpiryDate;

                        output.Visible = true;

                        output.Text = expiryDate.ToString();




            catch (Exception ex)


                DotNetNuke.UI.Skins.Skin.AddModuleMessage(this, ex.ToString(), DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.RedError);


New Post
8/26/2014 4:00 AM


After AddUserRole method you can try for UserLogin method again.
You can refer to this link (

Sibabrata Dash
Mindfire Solutions

New Post
8/26/2014 1:35 PM

Hi Sunil,

Thanks, although that doesn't seem to be working for me.

I switched this out: DotNetNuke.Security.PortalSecurity.ClearRoles();

For this: UserController.UserLogin(this.PortalId, objUser, PortalSettings.PortalName, this.Request.UserHostAddress, false);

I still only have the two roles until a second page refresh.

I wonder if this is a timing issue?

New Post
8/27/2014 6:12 AM
Could it be that you haver pseudo code that works like this...

control.DataBind(); // therefore showing the OLD values
object.UpdateRolesInSomeWay() // Adding the new role - but too late for it to get shown on the screen

That could cause it to show up on the next time around.

Best wishes,
- Richard
Agile Development Consultant, Practitioner, and Trainer
HomeHomeDevelopment and...Development and...Building ExtensionsBuilding ExtensionsModulesModulesProgrammatically adding User Roles Programmatically adding User Roles

These Forums are dedicated to discussion of DNN Platform and Evoq Solutions.

For the benefit of the community and to protect the integrity of the ecosystem, please observe the following posting guidelines:

  1. No Advertising. This includes promotion of commercial and non-commercial products or services which are not directly related to DNN.
  2. No vendor trolling / poaching. If someone posts about a vendor issue, allow the vendor or other customers to respond. Any post that looks like trolling / poaching will be removed.
  3. Discussion or promotion of DNN Platform product releases under a different brand name are strictly prohibited.
  4. No Flaming or Trolling.
  5. No Profanity, Racism, or Prejudice.
  6. Site Moderators have the final word on approving / removing a thread or post or comment.
  7. English language posting only, please.
Try Evoq
For Free
Start Free Trial
a Demo
See Evoq Live
Need More Information?