Search code examples
asp.net-mvcasp.net-identityidentityasp.net-identity-2

cannot convert from 'method group' to 'Func<ApplicationRoleManager> in asp mvc identity


this code for create a role in database .

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);
    }
}

but in Startup.Auth show me error :

enter image description here

How can I solve this error ?

Edit

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

Solution

  • Signature for CreatePerOwingContext<T> looks like this:

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

    or like this:

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

    I.e. you ether need to provide a function that will create you an object, i.e.

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

    This will be using second override. Or go back to defaults:

    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;
    }