19

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

I try do access it via IdentityStore but without success.

Nikolay Kostov
  • 16,433
  • 23
  • 85
  • 123
daniel
  • 34,281
  • 39
  • 104
  • 158

3 Answers3

29

This is a bit more intuitive

var roles = dbContext.Roles.OrderBy(x => x.Name);
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
joe
  • 1,851
  • 3
  • 17
  • 29
12

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);
}
Nikolay Kostov
  • 16,433
  • 23
  • 85
  • 123
5

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.

Hao Kung
  • 28,040
  • 6
  • 84
  • 93
  • 1
    Do you have any links or public info on the admin scenarios for Identity? Thanks. – danmiser Oct 18 '13 at 14:04
  • 2
    @Hao Kung "that will be coming in a future update targeting administration scenarios(post 1.0 RTM)" I'm currently building some admin pages for administrating users and roles. I see that the nightly builds have the class member Roles but the RTM hasn't. I was just wondering when the next update will be? Are we talking 3 months, 6 months, a year, longer? Thanks. – PussInBoots Nov 11 '13 at 09:53