Tuesday 9 September 2014

DNN: Programmatically Assign Role to User In Dotnetnuke

Assigning role to user programmatically can easily be done using DotNetNuke's RoleController in your code. It means without storing role info in database, and programmatically assign a role rights to the user. You can call AddUserRole function in RoleController to perform this action the below code might help you in achieving this

RoleController objRoles = new RoleController();
RoleInfo objRole = new RoleInfo;

// autoassign user to portal roles

var arrRoles = objRoles.GetPortalRoles(user.PortalID);

foreach (var obJrole in arrRoles) {
if (objRole.AutoAssignment == true) {
objRoles.AddUserRole(user.PortalID, user.UserID, objRole.RoleID, Null.NullDate, Null.NullDate); }

objRoles.AddUserRole(user.PortalID, user.UserID, 5, Null.NullDate, Null.NullDate); 


This post is from my old blog posted on May 2012, that unfortunately no longer exist.

About the Author

Adnan Zameer, Lead Developer at Optimizley UK, is a certified Microsoft professional, specializing in web app architecture. His expertise includes Optimizley CMS and Azure, showcasing proficiency in crafting robust and efficient solutions.


Post a Comment