1

在我的项目中添加角色时出现错误。请任何人帮助我。

身份配置文件(角色管理器)

 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>()));
            return new ApplicationRoleManager(new RoleStore<IdentityRole>(new ApplicationDbContext()));
        }
    }

配置身份验证

app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);

控制器

private ApplicationRoleManager _roleManager;

public ApplicationRoleManager RoleManager
{
    get
    {
        return _roleManager ?? HttpContext.GetOwinContext().Get<ApplicationRoleManager>();
    }
    private set
    {
        _roleManager = value;
    }
}

获取方法

public ActionResult Register()
        {
            var model = new RegisterViewModel();
            model.RoleList = RoleManager.Roles.Select(c => c.Name).ToList();
            return View();
        }

看法

    <div class="form-group">
        @Html.LabelFor(m => m.Role, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.DropDownListFor(m => m.Role, new SelectList(Model.RoleList), new { @class = "form-control" })
        </div>
    </div>

在此处输入图像描述时出错

4

0 回答 0