Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • AbsolutelyN 68 posts 393 karma points
    6 days ago
    AbsolutelyN
    0

    Member roles from FeatureManagement ITargetingContextAccessor?

    I'm currently setting up Microsoft.FeatureManagement on a v10 site.

    Ideally I'd like to target members by group but I'm not certain of the best way to access member roles.

    What's the best way to access member roles in a ITargetingContextAccessor?

    public class HttpTargetingContextAccessor : ITargetingContextAccessor
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly ILogger<HttpTargetingContextAccessor> _logger;
    
        public HttpTargetingContextAccessor(IHttpContextAccessor httpContextAccessor, ILogger<HttpTargetingContextAccessor> logger)
        {
            _httpContextAccessor = httpContextAccessor;
            _logger = logger;
        }
    
        public ValueTask<TargetingContext> GetContextAsync()
        {
            HttpContext httpContext = _httpContextAccessor.HttpContext;
            ClaimsPrincipal user = httpContext.User;
    
            TargetingContext targetingContext = new TargetingContext
            {
                UserId = user.Identity.Name,
                Groups = // ??
            }
            return new ValueTask<TargetingContext>(targetingContext);
        }
    

    Looping roles in claims returns nothing. And I can't inject MemberManager due to scoping. I assume accessing via MemberService would be expensive.

       private IEnumerable<string> GetGroupsFromClaims(ClaimsPrincipal user)
        {
            foreach(var claim in user.Claims)
            {
                if(claim.Type == ClaimTypes.Role)
                {
                    yield return claim.Value;
                }
            }
        }
    

    Any thoughts much appreciated!

  • AbsolutelyN 68 posts 393 karma points
    6 days ago
    AbsolutelyN
    0

    If anyone has a better solution please let me know:

    public class UmbracoMemberTargetingContextAccessor : ITargetingContextAccessor
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly ILogger<HttpTargetingContextAccessor> _logger;
        private readonly IMemberService _memberService;
    
        public HttpTargetingContextAccessor(IHttpContextAccessor httpContextAccessor, ILogger<HttpTargetingContextAccessor> logger, IMemberService memberService)
        {
            _httpContextAccessor = httpContextAccessor;
            _logger = logger;
            _memberService = memberService;
        }
    
        public ValueTask<TargetingContext> GetContextAsync()
        {
            HttpContext httpContext = _httpContextAccessor.HttpContext;
            ClaimsPrincipal user = httpContext.User;
    
            TargetingContext targetingContext = new TargetingContext
            {
                UserId = user.Identity.Name,
                Groups = GetUmbracoMemberGroups(user)
            };
    
            return new ValueTask<TargetingContext>(targetingContext);
        }
    
        private string GetClaimsIdentifier(ClaimsPrincipal user)
        {
            foreach (var claim in user.Claims)
            {
                if (claim.Type == ClaimTypes.NameIdentifier)
                {
                    return claim.Value;
                }
            }
            return "";
        }
    
        private IEnumerable<string> GetUmbracoMemberGroups(ClaimsPrincipal user)
        {
            var id = GetClaimsIdentifier(user);
            if (!string.IsNullOrEmpty(id))
            {
                return _memberService.GetAllRoles(Convert.ToInt32(id));
            }
            return new List<string>();
        }
    

    }

Please Sign in or register to post replies

Write your reply to:

Draft