V13 - Get member and roles without hitting databas...
# help-with-umbraco
i
I'm building a site that uses membership quite heavily. We have a lot of calls to check if a member is in a particular role, and am using this sort of code to do it:
Copy code
public bool IsEngineer(string objectIdentifier)
{
    var member = _memberService.GetByUsername(objectIdentifier);
    if (member != null)
    {
        return _memberService.GetAllRoles(objectIdentifier).Any(r => r.Equals(Constants.MemberGroups.Engineer));
    }

    return false;
}
looking at the Umbraco source code, this seems to be hitting the database everytime, so I'm thinking this is the wrong way of doing it? Ideas?
m
IIRC Umbraco ties into Microsofts authentication so everything should be in Claims Not sure if this is the correct interface but looks like it IMemberManager - https://github.com/umbraco/Umbraco-CMS/blob/3157601724cd3780c38491b5593338c1e718ab84/src/Umbraco.Web.Common/Security/MemberManager.cs
IsMemberAuthorizedAsync
a
You could also use
await memberManager.GetCurrentMemberAsync();
But the member part of Umbraco definitly needs work..
var member =  await memberManager.GetCurrentMemberAsync();
returns a MemberIdentityUser, but member.Roles contains no Roles... So you need to use
var memberGroups = await memberManager.GetRolesAsync(member);
but this gets a list of
string
. No objects with a name and an ID So if you somewhere else use a memberGroup picker, that value is stored as an
Id
.. So you then need to get
var memberGroups = memberGroupService.GetByIds(memberGroupsIds).Select(group => group.Name);
to match them again xD
m
IsMemberAuthorizedAsync
checks roles I believe
public virtual async Task IsMemberAuthorizedAsync( IEnumerable? allowTypes = null, IEnumerable? allowGroups = null, IEnumerable? allowMembers = null)
a
Ah so thats a bit better atleast 🙂
i
@Matt Wise I'd looked at the IMemberManager but forgotten about IsMemberAuthorizedAsync, that might just do the trick !!