ASP.NET Identity Manager Error: error when trying

2019-06-24 02:11发布

问题:

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