Membership


List of examples:

Creating a new membership



// Creates a new membership object
MembershipInfo newMembership = new MembershipInfo();

// Sets the membership properties
newMembership.MembershipDisplayName = "New membership";
newMembership.MembershipName = "NewMembership";
newMembership.MembershipSiteID = SiteContext.CurrentSiteID;

// Saves the membership to the database
MembershipInfo.Provider.Set(newMembership);

> Back to list of examples

Updating an existing membership



// Gets the membership
MembershipInfo updateMembership = MembershipInfo.Provider.Get("NewMembership", SiteContext.CurrentSiteID);
if (updateMembership != null)
{
    // Updates the membership properties
    updateMembership.MembershipDisplayName = updateMembership.MembershipDisplayName.ToLower();

    // Saves the changes to the database
    MembershipInfo.Provider.Set(updateMembership);
}

> Back to list of examples

Updating multiple memberships



// Gets all memberships whose name starts with 'NewMembership'
var memberships = MembershipInfo.Provider.Get().WhereStartsWith("MembershipName", "NewMembership");

// Loops through individual memberships
foreach (MembershipInfo modifyMembership in memberships)
{
    // Updates the  membership properties
    modifyMembership.MembershipDisplayName = modifyMembership.MembershipDisplayName.ToUpper();

    // Saves the changes
    MembershipInfo.Provider.Set(modifyMembership);
}

> Back to list of examples

Deleting a membership



// Gets the membership
MembershipInfo deleteMembership = MembershipInfo.Provider.Get("NewMembership", SiteContext.CurrentSiteID);

if (deleteMembership != null)
{
    // Deletes the membership
    MembershipInfo.Provider.Delete(deleteMembership);
}

> Back to list of examples

Including a role in a membership



// Gets role and membership objects
RoleInfo role = RoleInfo.Provider.Get("Rolename", SiteContext.CurrentSiteID);
MembershipInfo membership = MembershipInfo.Provider.Get("NewMembership", SiteContext.CurrentSiteID);

if ((role != null) && (membership != null))
{             
    // Adds the role to the membership
    MembershipRoleInfo.Provider.Add(membership.MembershipID, role.RoleID);
}

> Back to list of examples

Removing a role from a membership



// Gets role and membership objects
RoleInfo role = RoleInfo.Provider.Get("Rolename", SiteContext.CurrentSiteID);
MembershipInfo membership = MembershipInfo.Provider.Get("NewMembership", SiteContext.CurrentSiteID);

if ((role != null) && (membership != null))
{
    // Removes the role from the membership
    MembershipRoleInfo.Provider.Remove(membership.MembershipID, role.RoleID);
}

> Back to list of examples

Assigning a membership to a user



// Gets user and membership objects
UserInfo user = UserInfo.Provider.Get("Username");
MembershipInfo membership = MembershipInfo.Provider.Get("NewMembership", SiteContext.CurrentSiteID);

if ((user != null) && (membership != null))
{
    // Grants the membership to the user (valid for 3 minutes)
    MembershipUserInfo.Provider.Add(membership.MembershipID, user.UserID, DateTime.Now.AddMinutes(3));
}

> Back to list of examples

Removing a membership from a user



// Gets user and membership objects
UserInfo user = UserInfo.Provider.Get("Username");
MembershipInfo membership = MembershipInfo.Provider.Get("NewMembership", SiteContext.CurrentSiteID);

if ((user != null) && (membership != null))
{
    // Removes the membership from the user
    MembershipUserInfo.Provider.Remove(membership.MembershipID, user.UserID);
}

> Back to list of examples

Getting all valid memberships of a user



// Gets the current user
UserInfo user = MembershipContext.AuthenticatedUser;

if (user != null)
{
    // Gets all valid memberships assigned to the user.
    // Requires a using statement for the 'System.Linq' namespace.
    // The boolean parameter of the IsInMembership method is set to true,
    // so both memberships on the current site and global memberships are loaded.
    var memberships = MembershipInfo.Provider.Get()
        .Where(membership => user.IsInMembership(membership.MembershipName, SiteContext.CurrentSiteName, true));

    // Loops through the memberships
    foreach (MembershipInfo membership in memberships)
    {
        // Process the membership
    }
}

> Back to list of examples