试图获得身份中的所有角色 [英] Trying to get all roles in Identity

查看:72
本文介绍了试图获得身份中的所有角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取应用程序中所有角色的列表.我查看了以下帖子获取所有用户. 和其他来源.这是我认为应该执行的代码.

I am trying to get a list of all the roles in my application. I have looked at the following post Getting All Users... and other sources. Here is my code which I think is what I am supposed to do.

var roleStore = new RoleStore<IdentityRole>(context)
var roleMngr  = new RoleManager<IdentityRole>(roleStore);
List<string> roles = roleMngr.Roles.ToList();

但是,出现以下错误:无法将类型GenericList(IdentityRole)隐式转换为List(string).有什么建议?我正在尝试获取列表,以便可以在注册页面上填充一个下拉列表,以将用户分配给特定角色.我认为是使用ASPNet 4.5和身份框架2.

However, I’m getting the following error: Cannot implicitly convert type GenericList(IdentityRole) to List(string). Any suggestions? I am trying to get the list so I can populate a dropdown list on a registration page to assign a user to a particular role. Using ASPNet 4.5 and identity framework 2 (I think).

PS,我也尝试了Roles.GetAllRoles方法,但没有成功.

PS I’ve also tried the Roles.GetAllRoles method with no success.

推荐答案

看看您的参考链接并对其进行自我质疑,很明显,角色管理器(roleMngr)是IdentityRole的类型,因此角色必须相同如果您想获取角色列表,请输入.

Looking at your reference link and question it self, it is clear that the role manager (roleMngr) is type of IdentityRole, so that roles has to be the same type if you trying to get the list of roles.

使用由List<string>插入的var或使用List<IdentityRole>.

Use var insted of List<string> or use List<IdentityRole>.

var roleStore = new RoleStore<IdentityRole>(context);
var roleMngr = new RoleManager<IdentityRole>(roleStore); 

var roles = roleMngr.Roles.ToList();

希望这会有所帮助.

这篇关于试图获得身份中的所有角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆