0

I have a ASP.NET Identity initialization and I want to move it to the Autofac module. I searched the Internet but didn't find any solution. So, I have this in my Starup:

 public void ConfigureServices(IServiceCollection services)
    {
        /*SOME LOGIC
        .................*/
       
        services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = false)
            .AddRoles<IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>();
        
        services.AddOptions();
        services.AddRazorPages();
        services.AddControllersWithViews();
    }

And I want to move services.AddDefaultIdentity... to the Autofac module so the builder can register it instead of default services. I've tried the solution like this:

public class IdentityModule : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        builder.RegisterType<HttpContextAccessor>().As<IHttpContextAccessor>().SingleInstance();
        builder.Register(c => new UserStore<IdentityUser>(c.Resolve<ApplicationDbContext>())).AsImplementedInterfaces().SingleInstance();
        builder.Register(c => new RoleStore<IdentityRole>(c.Resolve<ApplicationDbContext>())).AsImplementedInterfaces().SingleInstance();
        builder.Register(c => new ClaimsPrincipal(c.Resolve<IHttpContextAccessor>().HttpContext?.User)).As<IPrincipal>().InstancePerRequest();
        builder.RegisterType<UserManager<IdentityUser>>().AsSelf().SingleInstance();
        builder.RegisterType<RoleManager<IdentityRole>>().AsSelf().SingleInstance();
        builder.RegisterType<SignInManager<IdentityUser>>().AsSelf().SingleInstance();
    }
}

But there is a call to User.IsInRole() in some of my views and there is an exception that I cannot solve.

UPD1: Added an exception

DependencyResolutionException: None of the constructors found with 
'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 
'Microsoft.AspNetCore.Identity.UserManager`1[Microsoft.AspNetCore.Identity.IdentityUser]' 
can be invoked with the available services and parameters: Cannot resolve parameter 
'Microsoft.AspNetCore.Identity.IPasswordHasher`1[Microsoft.AspNetCore.Identity.IdentityUser
] passwordHasher' of constructor 'Void 
.ctor(Microsoft.AspNetCore.Identity.IUserStore`1[Microsoft.AspNetCore.Identity.IdentityUser
], Microsoft.Extensions.Options.IOptions`1[Microsoft.AspNetCore.Identity.IdentityOptions], 
Microsoft.AspNetCore.Identity.IPasswordHasher`1[Microsoft.AspNetCore.Identity.IdentityUser, 
System.Collections.Generic.IEnumerable`1[Microsoft.AspNetCore.Identity.IUserValidator`1[Mic
rosoft.AspNetCore.Identity.IdentityUser]], 
System.Collections.Generic.IEnumerable`1[Microsoft.AspNetCore.Identity.IPasswordValidator`1
[Microsoft.AspNetCore.Identity.IdentityUser]], 
Microsoft.AspNetCore.Identity.ILookupNormalizer, 
Microsoft.AspNetCore.Identity.IdentityErrorDescriber, System.IServiceProvider, 
Microsoft.Extensions.Logging.ILogger`1[Microsoft.AspNetCore.Identity.UserManager`1[Microsof
t.AspNetCore.Identity.IdentityUser]])'.

And this exception too

DependencyResolutionException: An exception was thrown while activating 
Microsoft.AspNetCore.Identity.UserManager`1[[Microsoft.AspNetCore.Identity.IdentityUser, 
Microsoft.Extensions.Identity.Stores, Version=5.0.0.0, Culture=neutral, 
PublicKeyToken=adb9793829ddae60]].

UPD2: I began to take to pieces the AddDefaultIdentity method and it contains the following:

public static IdentityBuilder AddDefaultIdentity<TUser>(this IServiceCollection services, Action<IdentityOptions> configureOptions) where TUser : class
    {
        services.AddAuthentication(o =>
        {
            o.DefaultScheme = IdentityConstants.ApplicationScheme;
            o.DefaultSignInScheme = IdentityConstants.ExternalScheme;
        })
        .AddIdentityCookies(o => { });

        return services.AddIdentityCore<TUser>(o =>
        {
            o.Stores.MaxLengthForKeys = 128;
            configureOptions?.Invoke(o);
        })
            .AddDefaultUI()
            .AddDefaultTokenProviders();
    }

I managed with the services.AddIdentityCore by registering in builder the UserManager and all it contains in constructor like that:

builder.Register<UserStore<IdentityUser>>(c => c.Resolve<ApplicationDbContext>())

Website started to work but register and login pages don't oped without rewriting the services.AddAuthentication method, I think. I didn't done that because in contains a lot of internal classes and interfaces, and the logic is more complicated than in the initialization and work of UserManager. I don't know what to do next.

Hope you will help me.

0 Answers0