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:
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:
@{
var memberById = await _memberManager.FindByIdAsync("1234");
// Do stuff with the member, for instance checking if email is confirmed
var emailConfirmed = memberById.EmailConfirmed;
}
If we want to find a member by Udi or Guid we need to to inject IIdKeyMap service:
Find member by Udi
var memberUdiAttempt = _idKeyMap.GetIdForUdi(nodeUdi);
if (memberUdiAttempt.Success)
{
var memberId = memberUdiAttempt.Result;
var member = await _memberManager.FindByIdAsync(memberId.ToString());
}
Find member by Guid
var memberKeyAttempt = _idKeyMap.GetIdForKey(nodeKey);
if (memberKeyAttempt.Success)
{
var memberId = memberKeyAttempt.Result;
var member = await _memberManager.FindByIdAsync(memberId.ToString());
}
FindByEmailAsync(string)
Finds a member by their email.
@{
var memberById = await _memberManager.FindByEmailAsync("test@member.com");
// Do stuff with the member, for instance checking if email is confirmed
var emailConfirmed = memberById.EmailConfirmed;
}
FindByNameAsync(string)
Finds a member by their login name.
@{
var memberById = await _memberManager.FindByNameAsync("TestLoginName");
// Do stuff with the member, for instance checking if email is confirmed
var emailConfirmed = memberById.EmailConfirmed;
}
AsPublishedMember(MemberIdentityUser)
By default IMemberManager returns members as MemberIdentityUser. This method allows you to convert a MemberIndentityUser into IPublishedContent:
Returns the currently logged in member if there is one, else returns null value.
@{
var currentMember = await _memberManager.GetCurrentMemberAsync();
}
@if (currentMember is not null)
{
<p>A member is logged in, member username: @currentMember.UserName</p>
}
else
{
<p>No member is logged in.</p>
}
GetUserIdAsync()
Returns the user id of a user
@{
var userId = await _memberManager.GetUserIdAsync(user);
}
IsLoggedIn()
Checks if a member is logged in.
@if (_memberManager.IsLoggedIn())
{
<p>A member is logged in</p>
}
else
{
<p>No member is logged in.</p>
}
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:
@{
var memberIsAuthorized = await _memberManager.IsMemberAuthorizedAsync(allowGroups: new []{"VIP"});
}
IsProtectedAsync()
Returns a Task<bool> specifying if the page with a given Umbraco path has public access restrictions set.
<ul>@foreach (var child inModel.Children) {@if (await_memberManager.IsProtectedAsync(child.Path)) {<li>@child.Name- Members only!</li> }else {<li>@child.Name- Access to everyone!</li> } }</ul>
MemberHasAccessAsync(string)
Returns a Task<bool> specifying if the currently logged in member has access to the page given its Umbraco path.
<ul>@foreach (var child inModel.Children) { // Only display the page if the current member has access to it.@if (await_memberManager.MemberHasAccessAsync(child.Path)) {<li>@child.Name</li> } }</ul>