简体   繁体   中英

Adding custom role to user ASP.NET Identity

Context of problem

In my web application I've been using ASP.NET Identity. I've created custom user profiles by inheriting the User class from IdentityUser. To be able to connect the Roles with for example rolegroups I've also created a custom role class within my database which inherits from the IdentityRole class.

My database model looks like this: 数据库模型的一部分

The problem isn't in creating a user. This just works:

var userManager = new UserManager<User>(new UserStore<User>(_context));
var result = userManager.Create(user, password);

Same with creating a role.

var rm = new RoleManager<Role>(new RoleStore<Role>(_context));
var result = rm.Create(new Role(name));

Problem

But its when I try to attach a role to a user ( With the following code )

var um = new UserManager<User>(new UserStore<User>(_context));
var result = um.AddToRole(userId, roleName);

The error I get is basically " The entity type IdentityRole is not part of the model for the current context. "

Which is true, btw. But how can I make it that my Role entity is seen as the IdentityRole entity, which it basically is, or has to be.

See the full stacktrace here: http://pastebin.com/sEPv5LiT

Thanks in advance!

As i understand you want to define custom roles for users i prefer to add library project in your solution and manage all membership and roles through this.

Custom Asp.Net Membership

I make library project for that if you want i will provide you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM