Tuesday, 9 September 2014

How to Programmatically Assign a Role to a User in DNN

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); 

About the Author

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

0 comments :

Post a Comment