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
MembershipInfoProvider.SetMembershipInfo(newMembership);

> Back to list of examples

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);
}

> Back to list of examples

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);
}

> Back to list of examples

Deleting a membership

// Gets the membership
MembershipInfo deleteMembership = MembershipInfoProvider.GetMembershipInfo("NewMembership", SiteContext.CurrentSiteName);

if (deleteMembership != null)
{
	// Deletes the membership
	MembershipInfoProvider.DeleteMembershipInfo(deleteMembership);
}

> Back to list of examples

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);
}

> Back to list of examples

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);
}

> Back to list of examples

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));
}

> Back to list of examples

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);
}

> 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 = MembershipInfoProvider.GetMemberships()
		.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