无法在asp mvc标识中从'方法组'转换为'Func <applicationrolemanager>

时间:2016-10-18 07:32:04

标签: asp.net-mvc asp.net-identity identity asp.net-identity-2

此代码用于在数据库中创建角色。

IdentityConfig:

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

    public static ApplicationRoleManager Create(IOwinContext context)
    {
        var roleStore = new RoleStore<IdentityRole>(context.Get<ApplicationDbContext>());
        return new ApplicationRoleManager(roleStore);
    }
}

但在Startup.Auth中显示错误:

enter image description here

如何解决此错误?

修改

public void Configuration(IAppBuilder app)
{
    ConfigureAuth(app);
}

1 个答案:

答案 0 :(得分:1)

CreatePerOwingContext<T>的签名如下所示:

public static IAppBuilder CreatePerOwinContext<T>(this IAppBuilder app, Func<IdentityFactoryOptions<T>, IOwinContext, T> createCallback) where T : class, IDisposable

或者像这样:

public static IAppBuilder CreatePerOwinContext<T>(this IAppBuilder app, Func<T> createCallback) where T : class, IDisposable

即。你以太需要提供一个能够创建一个对象的函数,即

app.CreatePerOwinContext<ApplicationRoleManager>(() => new ApplicationRoleManager(/*blah params*/));

这将使用第二次覆盖。或者回到默认值:

public static ApplicationRoleManager Create(IdentityFactoryOptions<ApplicationRoleManager> options, IOwinContext context)
{
    var applicationDbContext = context.Get<ApplicationDbContext>();
    var roleStore = new RoleStore<IdentityRole>(applicationDbContext);
    var manager = new ApplicationRoleManager(roleStore);

    return manager;
}