ASP.NET Identity Manager Error: error when trying to create a controller of type 'MetaController' (no parameterless public constructor)

若如初见. 提交于 2019-12-10 15:15:21

问题


I got ThinkTecture's IdentityManager running, but now when going to the '/idm/ url I get an error:

An error occurred when trying to create a controller of type 'MetaController'. Make sure that the controller has a parameterless public constructor.

The error was mentioned in a comment in another StackOverflow issue but a solution to this issue was not given.


回答1:


While formulating this question I also found the solution in an issue of the IdentityManager GitHub repo. I had to change the constructor for ApplicationUserManager in IdentityConfig.cs from:

public ApplicationUserManager(IUserStore<ApplicationUser> store): base(store) {}

to:

public ApplicationUserManager(ApplicationUserStore store): base(store) {}

And a similar type change in the Create function just below that to get everything compiling.

The ApplicationUserStore should be defined as follows.

public class ApplicationUserStore: UserStore<ApplicationUser>
{
    public ApplicationUserStore(ApplicationDbContext ctx): base(ctx) {}
}

I put it in Startup.cs just above the declaration of ApplicationRoleStore.




回答2:


I had the same problem, but instead of modifying constructors, I fixed it by tweaking the DI registration

    public static void ConfigureMyCustomIdentityManagerService(this IdentityManagerServiceFactory factory, string connectionString)
    {
        factory.Register(new Registration<UserManager<User, Guid>>(x => new MyCustomUserManager(new MsSqlUserStore<User>(connectionString))));
        factory.Register(new Registration<RoleManager<Role, Guid>>(x => new RoleManager<Role, Guid>(new MsSqlRoleStore<Role>(connectionString))));

        factory.IdentityManagerService = new Registration<IIdentityManagerService, AspNetIdentityManagerService<User, Guid, Role, Guid>>();
    }


来源:https://stackoverflow.com/questions/33354145/asp-net-identity-manager-error-error-when-trying-to-create-a-controller-of-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!