Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
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!
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>(); }
}
is working on a reply...
Write your reply to:
Upload image
Image will be uploaded when post is submitted
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?
Looping roles in claims returns nothing. And I can't inject MemberManager due to scoping. I assume accessing via MemberService would be expensive.
Any thoughts much appreciated!
If anyone has a better solution please let me know:
}
is working on a reply...