Copied to clipboard

Flag this post as spam?

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


  • Ran Chen 40 posts 117 karma points
    Sep 07, 2015 @ 16:04
    Ran Chen
    0

    MemberService - get Roles by Member

    It sounds quite trivial to me , but going over MemberService API again and again , I still couldn't resolve this simple issue:

    How can I get a list of member's roles by this member Id (or user name) ?

    For now, I came up only with this solution which is time consuming:

    var roles = member_service.GetAllRoles(); 
    
        foreach (var role in roles)
        {
            if (member_service.FindMembersInRole(role, member.Username).Contains(member))
            {
                  //add this role to a list of this member roles ...
            }
        }
    

    Which is really stupid...

    Any better idea ?

  • Charles Afford 1163 posts 1709 karma points
    Sep 07, 2015 @ 16:26
    Charles Afford
    0

    I think what you need here is a linq statement. Have a look at the aggregate property on an ienumerable.

  • Ran Chen 40 posts 117 karma points
    Sep 07, 2015 @ 16:32
    Ran Chen
    0

    Could you please add an example ?

    thanks

  • Charles Afford 1163 posts 1709 karma points
    Sep 07, 2015 @ 16:36
    Charles Afford
    0

    How are you getting the members? And sure just trying to write one :)

  • Ran Chen 40 posts 117 karma points
    Sep 07, 2015 @ 16:41
    Ran Chen
    0

    I try to run this code on Razor, so I get the current logged in member:

    String user_name = Membership.GetUser().UserName;
        var member_service = ApplicationContext.Current.Services.MemberService;
        var member = member_service.GetByUsername(user_name);
    
  • Charles Afford 1163 posts 1709 karma points
    Sep 07, 2015 @ 16:52
    Charles Afford
    104

    Ok I see what you are doing. You can do this.

    Var rolesList = System.Web.Security.Roles.GetAllRolesForUser("userName")

  • Ran Chen 40 posts 117 karma points
    Sep 07, 2015 @ 19:40
    Ran Chen
    1

    High Five !

  • Charles Afford 1163 posts 1709 karma points
    Sep 07, 2015 @ 19:45
    Charles Afford
    0

    Thanks :D glad it worked

  • Niels Hartvig 1951 posts 2391 karma points c-trib
    Sep 07, 2015 @ 20:09
    Niels Hartvig
    2

    Love a thread like this - it's what makes Umbraco, Umbraco.

    Thanks Guys!

    /n

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies