MVC5 uses a new Identity System. How can I get all role names?
I try do access it via IdentityStore
but without success.
Show activity on this post. var user = await _userManager. FindByIdAsync(UserId); var roles = await _userManager. GetRolesAsync(user); return OK(new { User = user, Roles = roles });
GetUsersInRole(String) Method.
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.
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.
This is a bit more intuitive
var roles = dbContext.Roles.OrderBy(x => x.Name);
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);
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With