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.
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 confirmedvar emailConfirmed = member isnotnull&&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 confirmedvar emailConfirmed = member isnotnull&&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 confirmedvar emailConfirmed = member isnotnull&&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 isnotnull){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;
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.
publicasyncTask<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.