Asp.net mvc can';t返回AspNetRoles中的角色

Asp.net mvc can';t返回AspNetRoles中的角色,asp.net-mvc,asp.net-identity,identity,asp.net-identity-2,Asp.net Mvc,Asp.net Identity,Identity,Asp.net Identity 2,我需要返回标识选项卡中的所有角色以创建下拉列表 public class ApplicationRoleManager : RoleManager<IdentityRole> { public ApplicationRoleManager(RoleStore<IdentityRole> store) : base(store) { } public static ApplicationRoleManager Cre

我需要返回标识选项卡中的所有角色以创建下拉列表

    public class ApplicationRoleManager : RoleManager<IdentityRole>
{
    public ApplicationRoleManager(RoleStore<IdentityRole> store)
        : base(store)
    {

    }
    public static ApplicationRoleManager Create(IOwinContext context)
    {
        var Store = new RoleStore<IdentityRole>(context.Get<ApplicationDbContext>());
        // Configure validation logic for usernames
        return new ApplicationRoleManager(Store);
    }
}
公共类应用程序RoleManager:RoleManager
{
公共应用程序RoleManager(RoleStore商店)
:基地(商店)
{
}
公共静态应用程序角色管理器创建(IOwinContext上下文)
{
var Store=new RoleStore(context.Get


/*******************************************************************************************************/

通过设置
ApplicationRoleManager
获取所有角色的过程如下(根据Microsoft found提供的身份示例)

将下面的代码添加到您的IdentityConfig.cs

public class ApplicationRoleManager : RoleManager<IdentityRole>
{
    public ApplicationRoleManager(IRoleStore<IdentityRole, string> roleStore)
        : base(roleStore)
    {
    }

    public static ApplicationRoleManager Create(IdentityFactoryOptions<ApplicationRoleManager> options, IOwinContext context)
    {
        return new ApplicationRoleManager(new RoleStore<IdentityRole>(context.Get<ApplicationDbContext>()));
    }
}
公共类应用程序RoleManager:RoleManager
{
公共应用程序角色管理器(IRoleStore角色存储库)
:底座(roleStore)
{
}
公共静态应用程序角色管理器创建(IdentityFactoryOptions选项,IOwinContext上下文)
{
返回新的ApplicationRoleManager(新的RoleStore(context.Get());
}
}
然后在Startup.Auth.cs中的RoleManager上下文中初始化每个OW的单个实例:

app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);
app.CreatePerOwinContext(ApplicationRoleManager.Create);
在要获取所有角色的控制器中,执行以下操作:

private ApplicationRoleManager _roleManager;
public ApplicationRoleManager RoleManager
{
    get
    {
        return _roleManager ?? HttpContext.GetOwinContext().Get<ApplicationRoleManager>();
    }
    private set
    {
        _roleManager = value;
    }
}
private applicationrolemanger\u rolemanger;
公共应用程序角色管理器角色管理器
{
得到
{
return _rolemager??HttpContext.GetOwinContext().Get();
}
专用设备
{
_角色管理器=值;
}
}
之后,您只需在任何操作方法中使用
rolemager.Roles
,即可获得所有角色

这个答案包含了让它工作所需的所有步骤,但是如果您仍然不清楚这个过程,请参考上面nuget包的链接