Adding Roles and Users dynamically (in the code)*

Creating roles and users in the code is same as any other kind of providers used, just rememeber to setup them in the web.config.
However here I'm just placing a small utility class as a simple demo.

public static class MembershipHelper {

    public static void CreateRole(string rolename) {

        System.Web.Security.Roles.CreateRole(rolename);
    }

    public static void CreateUser(string username, string password) {

        System.Web.Security.Membership.CreateUser(username, password);
    }

    public static void CreateUserToRole(string username, string password, string rolename) {

        System.Web.Security.Membership.CreateUser(username, password);
        if (!System.Web.Security.Roles.RoleExists(rolename))
            System.Web.Security.Roles.CreateRole(rolename);
        if (!System.Web.Security.Roles.IsUserInRole(username, rolename)) 
            System.Web.Security.Roles.AddUserToRole(username, rolename);
    }
}


For more detailed view just explore: System.Web.Security.Roles and System.Web.Security.Membership


Happy coding ...

Any smart ideas and comments are welcome. Please do not hesitate to question me
See more about me and my works under ArtemBG | WebUtil.net

Last edited Aug 31, 2007 at 6:49 AM by velio, version 5

Comments

No comments yet.