Membership
List of examples:
- Creating a new membership
- Updating an existing membership
- Updating multiple memberships
- Deleting a membership
- Including a role in a membership
- Removing a role from a membership
- Assigning a membership to a user
- Removing a membership from a user
- Getting all valid memberships of a user
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);
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);
}
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);
}
Deleting a membership
// Gets the membership
MembershipInfo deleteMembership = MembershipInfo.Provider.Get("NewMembership", SiteContext.CurrentSiteID);
if (deleteMembership != null)
{
// Deletes the membership
MembershipInfo.Provider.Delete(deleteMembership);
}
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);
}
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);
}
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));
}
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);
}
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
}
}