Get all role names in ASP.NET MVC5 Identity system

前端 未结 3 1059
时光取名叫无心
时光取名叫无心 2020-12-25 14:31

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

I try do access it via IdentityStore but without success.

相关标签:
3条回答
  • 2020-12-25 14:38

    This is a bit more intuitive

    var roles = dbContext.Roles.OrderBy(x => x.Name);
    
    0 讨论(0)
  • 2020-12-25 14:44

    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.

    0 讨论(0)
  • 2020-12-25 14:47

    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);
    }
    
    0 讨论(0)
提交回复
热议问题