skip to Main Content

Using webforms and Identity with vs2019, how do I iterate the currently logged on user’s roles? Preferably in vb as I’m finding the c# to vb converters to be hit and miss. This is a hyper simple test project created with "Individual User Accounts". There are no claims in the claims table (whatever they are).

2

Answers


  1. Chosen as BEST ANSWER

    I think this is solved - there are two parts. First, the rolemanager tag in web.config isn't needed, and probably gums up the works, it has to do with the older membership framework. Second I'd placed the code for testing roles on default.aspx, the startup page. I knew there would be no roles in place at that point, prior to the user login, but didn't expect the code to fail from that. Moving the code to another page shows that it works.


  2. Well, it is super trivial to get roles.

    You can do this:

        Dim MyRoles As New List(Of String)
    
        MyRoles = Roles.GetRolesForUser().ToList
    

    However, a HUGE MASSIVE LARGE SUPER BIG difference here is while getting the user roles is trivial?

    Setting up roles and implementing roles? Well, now that is the HUGE MASSIVE LARGE BIG HARD part.

    If you have roles for identity actually working? Then gold Olympic medal of honor for all your huge amounts of efforts and work can and should be award to you!

    On the other hand, if you question is how to implement roles for identity? Well, then we have a huge large massive incredible different question here.

    You can always check/look at the sql table, and see if roles exist, and AspNetUserRoles table has values in it.

    However, if no roles are in that table, and you don’t have Roles implemented, then you have a MUCH larger and FAR MORE difficult problem to solve. So, assuming you have roles setup and implemented? Then your question is easy, and trivial.

    And you could query the AspNetUserLogons table, and join it to the AspNetUserInRoles, which in turn could be joined to the Roles table like this:

    enter image description here

    So drop in a grid view control like this:

        <asp:GridView ID="GRolesThis" runat="server" CssClass="table" Width="300px"> 
        </asp:GridView>
        <br />
    

    and in code go:

        Dim strSQL As String
    
        strSQL = "SELECT AspNetUsers.Email, AspNetRoles.Name " &
                 "FROM  AspNetUsers " &
                 "LEFT JOIN AspNetUserRoles ON AspNetUsers.Id = AspNetUserRoles.UserId " &
                 "LEFT JOIN AspNetRoles ON AspNetRoles.Id = AspNetUserRoles.RoleId"
    
        Using conn As New SqlConnection(My.Settings.WAuth)
    
            Using cmdSQL As New SqlCommand(strSQL, conn)
                conn.Open()
                GRoles.DataSource = cmdSQL.ExecuteReader
                GRoles.DataBind()
            End Using
    
        End Using
    

    and you get this:

    enter image description here

    But, once again, the above assume you have roles up and working.

    On the other hand, perhaps you might want to ask how to get Roles working for identity working first. Once you get Roles working, then the above shows this is easy, but I am betting your REAL question is not how to get user roles, but in fact how to get roles working.

    Since getting the roles is oh so easy?

    Doubt that is your real question. But, assuming you do have Roles up and running?

    Then the above is your answer.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search