Custom Event not working

Hi I am developing a plug-in i need that when user will be created at a specific email condition user should be addeded into a custom role but my code is not working

private void Events_AfterCreate(UserAfterCreateEventArgs e)
{

Apis.Get<IRoleUsers>().AddUserToRole(
new RoleUserCreateOptions() { UserId = e.Id, RoleId = 19 }
);

}

what is the wrong with this code.

Parents Reply
  • You are likely running into a permission issue, most users will not have permission to add themselves to a Role (otherwise users would just be able to make themselves administrtators).  The AddUserToRole call will need to be run as a user that has permissions to add a user to a role.

    Apis.Get<IUsers>().RunAsUser("admin", () => {
        //run your code here that needs elevated permissions
    });

    Should also note that this:

    catch
    {
    throw new Exception();
    }

    Is going to hide the actual exception that occurred and not provide the details needed to resolve any issues.

Children