How to retrieved current published logged in member in generated model format

Hi,

I’m currently working on a login portal for users on an Umbraco V16 site. For this, I’m using Umbraco’s built-in member functionality.

However, I’ve added some custom fields to the member document type, and these are reflected in the generated Member model, which indirectly inherits from IPublishedContent.

I’ve now set up the following using the IMemberManager:

public async Task<Member?> GetCurrentPublishedMemberAsync()
{
    var identity = await _memberManager.GetCurrentMemberAsync();
    if (identity != null)
    {
        IPublishedContent? member = _memberManager.AsPublishedMember(identity);
        if (member != null)
        {
            if (member is Member castedMember)
                return castedMember;
        }
    }
    return null;
}

From what I remember, this setup used to work fine in V13, but unfortunately it no longer does.
Is there anyone who can help me with this?

It seems like the member is no longer of type Member.

Regards,
Joppe

Okay I think I found a solution

public class PublishedMembersAppService : IPublishedMembersAppService
{
    private readonly IMemberManager _memberManager;
    private readonly IMembersAppService _membersAppService;
    private readonly IPublishedModelFactory _modelFactory;

    public PublishedMembersAppService(IMemberManager memberManager, IMembersAppService membersAppService, IPublishedModelFactory modelFactory)
    {
        _memberManager = memberManager;
        _membersAppService = membersAppService;
        _modelFactory = modelFactory;
    }

    #region Public
    public async Task<Member?> GetCurrentPublishedMemberAsync()
    {
        var identity = await _memberManager.GetCurrentMemberAsync();
        if (identity != null)
        {
            IPublishedContent? member = _memberManager.AsPublishedMember(identity);
            if (member != null)
            {
                var typed = member.CreateModel(_modelFactory);
                if (typed is Member castedMember)
                {
                    return castedMember;
                }
            }
        }
        return null;
    }
    #endregion
}

But I dont think this is the best solution. Any other ideas?

Greetings,

Joppe

Hey Joppe,

Thanks for this post and your response!

I have come across the exact same situation when upgrading from 14.2 to 16.1 and your post helped solve my issue.

Unfortunately I don’t have a better solution, but I will keep an eye out here to see if there is one. I am intrigued as to why this functionality changed.

I would also love to know how you came up with your solution if you get time to explain.

Cheers, Ben.

Hi Joppe,

As @benmurphycb already said, you’ve helped me tremendously with this post. Thanks in advance for that.

Sadly I don’t have any other way to help you out, but I did create an Umbraco issue for this (given that it works the same for Content references.

Here’s the link to my issue on the Umbraco Github page: V16: Pattern matching from Member Picker value (IPublishedContent) to strongly typed Member model not working · Issue #20612 · umbraco/Umbraco-CMS · GitHub

Hope you’ll get your answer soon. For now this is the solution we need to make our logic work for Umbraco 16.

Thanks again.

Kind regards, Jos

Thanks Jos for making the issue on GitHub!

1 Like