How to add roles to members programmatically in Umbraco CMS

Let say we have a member with email umbraco-member@piotrbach.com

The system has two member groups named:

“Customers” and “Partners”

To add a member to both roles you can use a handy method called ‘AddUserToRoles’ available in ‘System.Web.Security’ namespace:

using System.Web.Security;

You need to specify the user name and desired membership groups (roles) array and that’s it.

Roles.AddUserToRoles("umbraco-member@piotrbach.com", new[] { "Customers", "Partners" });

Once you execute the code and navigate to Umbraco’s back-office, you should see that member was added to two member groups as expected:

Umbraco Member With Two Membership Groups
Umbraco back-office – member roles

Similar Posts