is a way add user role or user privilegies during the registration by the admin of the system

Bahavu Atosha 1 Reputation point
2021-11-06T08:15:28.44+00:00

i'm new in asp net core 5, so when i'm trying to register new customer it's working,
but the problem is, as the admin of the system i would like to assign a role to the customer like Admin,Manager,Head of Department and agent.
i would like to use entity framework to implement this.
can you help me please...??

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
698 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,207 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Zhi Lv - MSFT 32,021 Reputation points Microsoft Vendor
    2021-11-08T05:31:29.537+00:00

    Hi @Bahavu Atosha ,

    Generally, in asp.net core application, it will use the Asp.net core Identity to manager user and roles.

    I assume you are also using Asp.net core Identity to manage user and role, if that is the case, you can add Role services to Identity using the following code:

    public void ConfigureServices(IServiceCollection services)  
    {  
        services.AddDbContext<ApplicationDbContext>(options =>  
            options.UseSqlServer(  
                Configuration.GetConnectionString("DefaultConnection")));  
        services.AddDefaultIdentity<IdentityUser>()  
            .AddRoles<IdentityRole>()       // Append AddRoles to add Role services:  
            .AddEntityFrameworkStores<ApplicationDbContext>();  
      
        services.AddControllersWithViews();  
        services.AddRazorPages();  
    }  
    

    Or update the Identity service as below:

       services.AddIdentity<IdentityUser, IdentityRole>()  
            // services.AddDefaultIdentity<IdentityUser>()  
            .AddEntityFrameworkStores<ApplicationDbContext>()  
            .AddDefaultTokenProviders();  
    

    Then, you can use the RoleManager to add/delete role, and use the UserManager to assign a role to the specified user(check the RoleManager and UserManager's methods). Here is an article about How to work with Roles in ASP.NET Core Identity, you could refer it.

    Besides, if you want to add custom user data to the Identity table, you can refer Add, download, and delete custom user data to Identity in an ASP.NET Core project.
    And here is an article about Role-based authorization in ASP.NET Core and Policy-Based And Role-Based Authorization In ASP.NET Core 3.0 Using Custom Handler, they might help you manage user privileges.


    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