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.

For the full list of methods, see the IMemberManager Interface API Documentation.

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:

MemberAuthenticationController.cs
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:

MemberAuthenticationView.cshtml
@using Umbraco.Cms.Core.Security;
@inject IMemberManager _memberManager;
@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage

@if (_memberManager.IsLoggedIn())
{
    @* Do something when a member is logged in *@
}

It is advisable to implement Controllers to manage this kind of view logic.

Examples

Finding members

IMemberManager has multiple ways to find members.

FindByIdAsync(string)

Finds a member by their ID

var member = await _memberManager.FindByIdAsync("1234");
// Do stuff with the member, for instance checking if email is confirmed
var emailConfirmed = member is not null && member.EmailConfirmed;

If we want to find a member by Udi or Guid we need to inject IIdKeyMap service:

Find member by Udi

var memberUdiAttempt = _idKeyMap.GetIdForUdi(memberUdi);
if (memberUdiAttempt.Success)
{
    var memberId = memberUdiAttempt.Result;
    var member = await _memberManager.FindByIdAsync(memberId.ToString());
}

Find member by Guid

var memberKeyAttempt = _idKeyMap.GetIdForKey(memberKey, UmbracoObjectTypes.Member);
if (memberKeyAttempt.Success)
{
    var memberId = memberKeyAttempt.Result;
    var member = await _memberManager.FindByIdAsync(memberId.ToString());
}

FindByEmailAsync(string)

Finds a member by their email.

var member = await _memberManager.FindByEmailAsync("test@member.com");
// Do stuff with the member, for instance checking if email is confirmed
var emailConfirmed = member is not null && member.EmailConfirmed;

FindByNameAsync(string)

Finds a member by their login name.

var member = await _memberManager.FindByNameAsync("TestLoginName");
// Do stuff with the member, for instance checking if email is confirmed
var emailConfirmed = member is not null && member.EmailConfirmed;

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)::

MemberIdentityUser? member = await _memberManager.FindByEmailAsync("test@member.com");
if (member is not null)
{
    IPublishedContent? memberAsContent = _memberManager.AsPublishedMember(member);
    // Access member content properties
}

GetCurrentMemberAsync()

Returns the currently logged in member if there is one, else returns null value.

var currentMember = await _memberManager.GetCurrentMemberAsync();
var currentMemberName = currentMember?.Name;

GetUserIdAsync()

Returns the ID of a member.

public async Task<string> GetMemberId(MemberIdentityUser member)
    => await _memberManager.GetUserIdAsync(member);

IsLoggedIn()

Checks if the current request contains a logged-in member.

public async Task<string> GetMemberId(MemberIdentityUser member)
    => await _memberManager.GetUserIdAsync(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:

var memberIsVIP = await _memberManager.IsMemberAuthorizedAsync(allowGroups: new []{"VIP"});

IsProtectedAsync()

Returns a Task<bool> specifying if the content with a given Umbraco path has public access restrictions set.

public async Task<string> GetContentName(IPublishedContent content)
    => await _memberManager.IsProtectedAsync(content.Path)
        ? $"{content.Name} - Members only!"
        : $"{content.Name} - Access for everyone!";

MemberHasAccessAsync(string)

Returns a Task<bool> specifying if the currently logged in member has access to the content given its Umbraco path.

public async Task<string> GetContentName(IPublishedContent content)
    => await _memberManager.MemberHasAccessAsync(content.Path)
        ? content.Name
        : "Members only";

ValidateCredentialsAsync(string username, string password)

Validates that specific member credentials are correct (without performing a log-in).

public async Task<bool> IsValidCredentials(string userName, string password)
    => await _memberManager.ValidateCredentialsAsync(userName, password);

Last updated