Blazor Server - Edit specific data based on roles

Cenk 1,041 Reputation points
2022-07-06T14:23:45.277+00:00

Hi there,

I have a Blazor Server app and I have a list of inventories on a razor page. When a user selects a row and edits, I would like to check if this user has an appropriate role to update the data.

Here is a sample edit page;

218216-edit-specific-data-based-on-roles.png

Let's say this user can only edit the quantity. Is there a way to do this?

Thanks in advance.

Developer technologies | .NET | Blazor
0 comments No comments
{count} votes

Answer accepted by question author
  1. Anonymous
    2022-07-07T07:33:34.6+00:00

    Hi @Cenk ,

    Using <AuthorizeView> is also similar right?

    Yes, you are right. The AuthorizeView component supports role-based or policy-based authorization.

    For role-based authorization, use the Roles parameter:

    <AuthorizeView Roles="admin, superuser">  
        <p>You can only see this if you're an admin or superuser.</p>  
    </AuthorizeView>  
    

    More information, see Role-based and policy-based authorization.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 81,976 Reputation points Volunteer Moderator
    2022-07-06T16:58:22.973+00:00

    inject the user and role mangers then:

            @if (context.User.IsInRole("some role"))  
            {  
                <InputText id="foo" @bind-Value="model.foo" />  
            }  
            else  
            {  
                <span class="field">@model.foo</span>  
            }  
    
    0 comments No comments

  2. Cenk 1,041 Reputation points
    2022-07-06T17:05:48.17+00:00

    Thank you @Bruce (SqlWork.com) , displaying and hiding data based on roles is a clever way :) Using <AuthorizeView> is also similar right?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.