Copied to clipboard

Flag this post as spam?

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


  • Kap 21 posts 173 karma points
    Aug 04, 2024 @ 21:53
    Kap
    1

    RoleManager or another way of getting the current user's Roles in a Razor page.

    Hi All,

    I am trying to port an Umbraco 8 page to 13, and one of the features currently being used in the Razor page is

    System.Web.Security.Roles.GetRolesForUser

    Which returns a list of Roles assigned to the member.

    However this is not available under .Net Core. I understand that you can do something similar using RoleManager, however this does not appear to be available for injection into a Razor page.

    The alternative appears to be IMembershipRoleService, however I cannot find any information on how to use this.

    Has anyone done something similar?

    The only other option would be to access the Roles from within a Render Controller, and pass them to the View.

    Thanks

    Kap

  • Huw Reddick 1932 posts 6722 karma points MVP 3x c-trib
    Aug 05, 2024 @ 09:00
    Huw Reddick
    101

    You could just use the IMemberManager

    @inject IMemberManager _memberManager
    
    @{
    var currentUser = await _memberManager.GetUserAsync(Context.User);
    IList<string> roles = new List<string>();
    if (currentUser != null)
    {
        roles = await _memberManager.GetRolesAsync(currentUser);
    }
    }
    
  • Kap 21 posts 173 karma points
    Aug 05, 2024 @ 12:30
    Kap
    1

    Thanks Huw! You are a life saver! Worked perfectly.

    Thanks

    Kap.

  • 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