IMemberManager
Using the IMemberManager
IMemberManager is an user manager interface for accessing member data in the form of MemberIdentityUser and converting it to IPublishedContent. IMemberManager has a variety of methods that are useful in views and controllers. For the list of methods, see the IMemberManager Interface API Documentation.
How to reference IMemberManager
There are different ways to reference MembershipHelper:
Views
While working with templates, the methods are available when you inject @IMemberManager to access member data:
@using Umbraco.Cms.Core.Security;
@inject IMemberManager _memberManager;
_memberManager.IsLoggedIn()Dependency Injection
UmbracoAuthorizedApiControllerhas been removed from Umbraco 14. UseManagementApiControllerBaseclass instead.UmbracoApiControlleris obsolete in Umbraco 14 and will be removed in Umbraco 15.
If you wish to use the IMemberManager in a class that inherits from one of the Umbraco base classes (eg. SurfaceController, UmbracoApiController, or UmbracoAuthorizedApiController), you can use Dependency Injection. For instance, if you have registered your own class in Umbraco's dependency injection, you can specify the IMemberManager interface in your constructor:
public class MemberAuthenticationSurfaceController : SurfaceController
{
private readonly IMemberManager _memberManager;
public MemberAuthenticationSurfaceController(IMemberManager memberManager)
{
_memberManager = memberManager;
}
}Examples
Finding members
IMemberManager has multiple ways to find members.
FindByIdAsync(string)
Finds a member by their ID
If we want to find a member by Udi or Guid we need to to inject IIdKeyMap service:
Find member by Udi
Find member by Guid
FindByEmailAsync(string)
Finds a member by their email.
FindByNameAsync(string)
Finds a member by their login name.
AsPublishedMember(MemberIdentityUser)
By default IMemberManager returns members as MemberIdentityUser. This method allows you to convert a MemberIndentityUser into IPublishedContent:
GetCurrentMemberAsync()
Returns the currently logged in member if there is one, else returns null value.
GetUserIdAsync()
Returns the user id of a user
IsLoggedIn()
Checks if a member is logged in.
IsMemberAuthorizedAsync(IEnumerable memberTypes, IEnumerable memberGroups, IEnumerable memberIds)
Checks if the current member is authorized for content protected by types, groups or specific members. For instance, you can use this method to check if the current logged in member is authorized. This is particularly useful for pages only available to the VIP member group, like so:
IsProtectedAsync()
Returns a Task<bool> specifying if the page with a given Umbraco path has public access restrictions set.
MemberHasAccessAsync(string)
Returns a Task<bool> specifying if the currently logged in member has access to the page given its Umbraco path.
MemberManager can also be used to manage users.
ValidateCredentialsAsync(string username, string password)
Validates that a user's credentials are correct without logging them in.
Last updated