question

BahavuAtosha-8545 avatar image
0 Votes"
BahavuAtosha-8545 asked AgaveJoe edited

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

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...??

dotnet-aspnet-core-mvcdotnet-entity-framework-core
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Can you explain how your application works and what kind of application you are building? Are you using the Identity framework to manage accounts or did you create a custom solution?

As I understand, registration is working. Can you show us how your registration feature works?

0 Votes 0 ·

my registration works yes, but in the normal way the admin must be the only one able to add other users, so when he will be adding new user he has to choose if the user will be a manager, head of department or a simple agent.
I have an issue when trying to manage user privileges...
I want my admin to add new user in the system and during the registration he must be able to assign a role to the new user because there we will have action that a manager can make that a normal agent can't make.

0 Votes 0 ·

You did not answer any of the questions or provide sample code.

Keep in mind, adding records to a table is very simple and covered in every beginning level tutorial. To help you, the community needs to know how your application works and we need sample code that illustrates the issue.

Are you using Identity? Do you need help learning the Identity API?

Did you build a custom solution?

0 Votes 0 ·

1 Answer

ZhiLv-MSFT avatar image
0 Votes"
ZhiLv-MSFT answered

Hi @BahavuAtosha-8545,

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.