IMemberManager
Using the IMemberManager
IMemberManager has a variety of methods that are useful for managing members in controllers and views. In this article, we'll have a look at how some of these can be used.
How to reference IMemberManager
There are different ways to reference IMemberManager:
Dependency Injection
The recommended way is to create a Controller or Service and inject IMemberManager in the constructor:
using Microsoft.AspNetCore.Mvc;
using Umbraco.Cms.Core.Security;
namespace UmbracoDocs.Samples;
public class MemberAuthenticationController : Controller
{
private readonly IMemberManager _memberManager;
public MemberAuthenticationController(IMemberManager memberManager)
=> _memberManager = memberManager;
}Views
Alternatively, IMemberManager can be injected directly into a template:
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 inject IIdKeyMap service:
Find member by Udi
UdiFind member by Guid
GuidFindByEmailAsync(string)
Finds a member by their email.
FindByNameAsync(string)
Finds a member by their login name.
AsPublishedMember(MemberIdentityUser)
The IMemberManager methods returns members as MemberIdentityUser.
Since Members Types are defined like Content Types in Umbraco, members can hold any number of properties. To access these properties, it can be beneficial to convert the member into an IPublishedContent instance.
This is done using AsPublishedMember(MemberIdentityUser)::
GetCurrentMemberAsync()
Returns the currently logged in member if there is one, else returns null value.
GetUserIdAsync()
Returns the ID of a member.
IsLoggedIn()
Checks if the current request contains a logged-in member.
IsMemberAuthorizedAsync(IEnumerable memberTypes, IEnumerable memberGroups, IEnumerable memberIds)
Checks if the current member is authorized as specific member types, member groups or concrete members.
For instance, you can use this method to verify if the current logged in member is part of a specific group:
IsProtectedAsync()
Returns a Task<bool> specifying if the content 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 content given its Umbraco path.
ValidateCredentialsAsync(string username, string password)
Validates that specific member credentials are correct (without performing a log-in).
Last updated
Was this helpful?