Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all role names in ASP.NET MVC5 Identity system

MVC5 uses a new Identity System. How can I get all role names?

I try do access it via IdentityStore but without success.

like image 475
daniel Avatar asked Sep 05 '13 09:09

daniel


People also ask

How do I get user role in .NET core identity?

Show activity on this post. var user = await _userManager. FindByIdAsync(UserId); var roles = await _userManager. GetRolesAsync(user); return OK(new { User = user, Roles = roles });

Which method enables you to return a list of users in a role that has a particular username?

GetUsersInRole(String) Method.

What is AspNet identity?

ASP.NET Identity is Microsoft's user management library for ASP.NET. It includes functionality such as password hashing, password validation, user storage, and claims management. It usually also comes with some basic authentication, bringing its own cookies and multi-factor authentication to the party.

What is ApplicationDbContext in asp net?

The ApplicationDbContext links the database server to the data model classes in the Asp.Net application. This only calls dbContext. Table1. Field1 and has the value of a data table.


3 Answers

This is a bit more intuitive

var roles = dbContext.Roles.OrderBy(x => x.Name);
like image 94
joe Avatar answered Sep 21 '22 12:09

joe


I've found that you can use the DbContext via the IdentityStore instance and use the well-known method .Set<T>().

This works for me:

var identityStore = new IdentityStore();
foreach (var role in identityStore.DbContext.Set<Role>())
{
    Debug.WriteLine(role.Name);
}
like image 33
Nikolay Kostov Avatar answered Sep 22 '22 12:09

Nikolay Kostov


There's currently no way to do enumeration style methods via the identity interfaces, that will be coming in a future update targeting administration scenarios(post 1.0 RTM), so there's no way to enumerate all users or roles via the Identity APIs. That said, you can always drop down to EF or whatever the store implementation is to directly enumerate the roles/users.

like image 34
Hao Kung Avatar answered Sep 21 '22 12:09

Hao Kung