0

I want to use usermanager methods without add identity to whole project.Because my application already uses an authentication that is not identity. I just want to connect db and insert user via usermanager. But when i use it like this got di error.

Builder.cs

builder.Services.AddDbContext<CustomIdentityDbContext>(opt => { opt.UseSqlServer(builder.Configuration.GetConnectionString("SomeDb")); });

builder.Services.AddScoped<UserManager<CustomIdentityUser>>();

Service and injection:

   private readonly UserManager<CustomIdentityUser> _userManager;

    public TenantController(UserManager<CustomIdentityUser> userManager)
    {
        _userManager = userManager;
    }
Tuğba Sivri
  • 53
  • 1
  • 4
  • See if this link is helpful. https://stackoverflow.com/questions/38253607/creating-a-usermanager-outside-of-built-in-dependency-injection-system – SoftwareDveloper Aug 11 '23 at 17:56
  • "got di error". Please specify the error. Add all relevant error details to your question. If this is a runtime error, please show the exception message and post the full stacktrace. If this is a compile time error, please specify the compiler error message and the exact line on which the error occurs. – Steven Aug 11 '23 at 20:55
  • Its a runtime error that says: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.UserManager`1[Data.Entity.CustomIdentityUser] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.UserManager`1[Data.Entity.CustomIdentityUser]': Unable to resolve service for type 'Microsoft.AspNetCore.Identity.IUserStore`1[Data.Entity.CustomIdentityUser]' while attempting to activate 'Microsoft.AspNetCore.Identity.UserManager`1[Data.Entity.CustomIdentityUser]'.)" – Tuğba Sivri Aug 14 '23 at 07:50

1 Answers1

1

Because my application already uses an authentication that is not identity. I just want to connect db and insert user via usermanager.

Sounds like you want a pure solution that doesn't regist Identity related Authentication Schemes

Then let us check the source codes of AddDefaultIdentity:

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

You could see AddIdentityCore method is the possible solution for you

Then Let's check the codes inside AddIdentityCore method:

public static IdentityBuilder AddIdentityCore<TUser>(this IServiceCollection services, Action<IdentityOptions> setupAction)
        where TUser : class
    {
        // Services identity depends on
        services.AddOptions().AddLogging();

        // Services used by identity
        services.TryAddScoped<IUserValidator<TUser>, UserValidator<TUser>>();
        services.TryAddScoped<IPasswordValidator<TUser>, PasswordValidator<TUser>>();
        services.TryAddScoped<IPasswordHasher<TUser>, PasswordHasher<TUser>>();
        services.TryAddScoped<ILookupNormalizer, UpperInvariantLookupNormalizer>();
        services.TryAddScoped<IUserConfirmation<TUser>, DefaultUserConfirmation<TUser>>();
        // No interface for the error describer so we can add errors without rev'ing the interface
        services.TryAddScoped<IdentityErrorDescriber>();
        services.TryAddScoped<IUserClaimsPrincipalFactory<TUser>, UserClaimsPrincipalFactory<TUser>>();
        services.TryAddScoped<UserManager<TUser>>();

        if (setupAction != null)
        {
            services.Configure(setupAction);
        }

        return new IdentityBuilder(typeof(TUser), services);
    }

It adds plenty of services into Identity builder,when IServiceCollection was build,it would regist required service for you,that why you got DI error when you just try withbuilder.Services.AddScoped<UserManager<CustomIdentityUser>>();

Now,inject IAuthenticationSchemeProvider into PageModel and check the registed Authentication schemes with _authenticationSchemeProvider.GetAllSchemesAsync()

When I try with default Identity,4 schemes were registed: enter image description here

When I try with

builder.Services.AddIdentityCore<AppUser>().AddEntityFrameworkStores<IdentityTestContext>();

No Authentication scheme was registed and a record was created successfully in Database enter image description here

Ruikai Feng
  • 6,823
  • 1
  • 2
  • 11