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
MembershipInfoProvider.SetMembershipInfo(newMembership);
Updating an existing membership
// Gets the membership
MembershipInfo updateMembership = MembershipInfoProvider.GetMembershipInfo("NewMembership", SiteContext.CurrentSiteName);
if (updateMembership != null)
{
// Updates the membership properties
updateMembership.MembershipDisplayName = updateMembership.MembershipDisplayName.ToLower();
// Saves the changes to the database
MembershipInfoProvider.SetMembershipInfo(updateMembership);
}
Updating multiple memberships
// Gets all memberships whose name starts with 'NewMembership'
var memberships = MembershipInfoProvider.GetMemberships().WhereStartsWith("MembershipName", "NewMembership");
// Loops through individual memberships
foreach (MembershipInfo modifyMembership in memberships)
{
// Updates the membership properties
modifyMembership.MembershipDisplayName = modifyMembership.MembershipDisplayName.ToUpper();
// Saves the changes
MembershipInfoProvider.SetMembershipInfo(modifyMembership);
}
Deleting a membership
// Gets the membership
MembershipInfo deleteMembership = MembershipInfoProvider.GetMembershipInfo("NewMembership", SiteContext.CurrentSiteName);
if (deleteMembership != null)
{
// Deletes the membership
MembershipInfoProvider.DeleteMembershipInfo(deleteMembership);
}
Including a role in a membership
// Gets role and membership objects
RoleInfo role = RoleInfoProvider.GetRoleInfo("Rolename", SiteContext.CurrentSiteID);
MembershipInfo membership = MembershipInfoProvider.GetMembershipInfo("NewMembership", SiteContext.CurrentSiteName);
if ((role != null) && (membership != null))
{
// Adds the role to the membership
MembershipRoleInfoProvider.AddMembershipToRole(membership.MembershipID, role.RoleID);
}
Removing a role from a membership
// Gets role and membership objects
RoleInfo role = RoleInfoProvider.GetRoleInfo("Rolename", SiteContext.CurrentSiteID);
MembershipInfo membership = MembershipInfoProvider.GetMembershipInfo("NewMembership", SiteContext.CurrentSiteName);
if ((role != null) && (membership != null))
{
// Removes the role from the membership
MembershipRoleInfoProvider.RemoveMembershipFromRole(membership.MembershipID, role.RoleID);
}
Assigning a membership to a user
// Gets user and membership objects
UserInfo user = UserInfoProvider.GetUserInfo("Username");
MembershipInfo membership = MembershipInfoProvider.GetMembershipInfo("NewMembership", SiteContext.CurrentSiteName);
if ((user != null) && (membership != null))
{
// Grants the membership to the user (valid for 3 minutes)
MembershipUserInfoProvider.AddMembershipToUser(membership.MembershipID, user.UserID, DateTime.Now.AddMinutes(3));
}
Removing a membership from a user
// Gets user and membership objects
UserInfo user = UserInfoProvider.GetUserInfo("Username");
MembershipInfo membership = MembershipInfoProvider.GetMembershipInfo("NewMembership", SiteContext.CurrentSiteName);
if ((user != null) && (membership != null))
{
// Removes the membership from the user
MembershipUserInfoProvider.RemoveMembershipFromUser(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 = MembershipInfoProvider.GetMemberships()
.Where(membership => user.IsInMembership(membership.MembershipName, SiteContext.CurrentSiteName, true));
// Loops through the memberships
foreach (MembershipInfo membership in memberships)
{
// Process the membership
}
}