C# 获取 ASP.NET MVC5 身份系统中的所有角色名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18633040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:48:23  来源:igfitidea点击:

Get all role names in ASP.NET MVC5 Identity system

c#asp.net-mvcasp.net-mvc-5asp.net-identity

提问by daniel

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

MVC5 使用新的身份系统。如何获取所有角色名称?

I try do access it via IdentityStorebut without success.

我尝试通过访问它IdentityStore但没有成功。

采纳答案by Nikolay Kostov

I've found that you can use the DbContextvia the IdentityStoreinstance and use the well-known method .Set<T>().

我发现您可以使用DbContextviaIdentityStore实例并使用众所周知的 method .Set<T>()

This works for me:

这对我有用:

var identityStore = new IdentityStore();
foreach (var role in identityStore.DbContext.Set<Role>())
{
    Debug.WriteLine(role.Name);
}

回答by Hao Kung

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.

目前无法通过身份接口执行枚举样式方法,这将在未来针对管理场景(RTM 1.0 之后)的更新中出现,因此无法通过身份 API 枚举所有用户或角色。也就是说,您始终可以下拉到 EF 或任何商店实现来直接枚举角色/用户。

回答by joe

This is a bit more intuitive

这个比较直观

var roles = dbContext.Roles.OrderBy(x => x.Name);