Copied to clipboard

Flag this post as spam?

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


  • Kap 19 posts 169 karma points
    Aug 04, 2024 @ 21:53
    Kap
    0

    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 1929 posts 6697 karma points MVP 2x c-trib
    Aug 05, 2024 @ 09:00
    Huw Reddick
    100

    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 19 posts 169 karma points
    Aug 05, 2024 @ 12:30
    Kap
    0

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

    Thanks

    Kap.

Please Sign in or register to post replies

Write your reply to:

Draft