.Net Core 不使用AddIdentity注入UserManager

问题描述 投票:0回答:1

我想使用 usermanager 方法而不向整个项目添加身份。因为我的应用程序已经使用了非身份的身份验证。我只想连接数据库并通过用户管理器插入用户。但是当我像这样使用它时出现错误。

Builder.cs

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

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

服务与注射:

   private readonly UserManager<CustomIdentityUser> _userManager;

    public TenantController(UserManager<CustomIdentityUser> userManager)
    {
        _userManager = userManager;
    }
c# .net-core dependency-injection asp.net-core-mvc asp.net-identity
1个回答
0
投票

因为我的应用程序已经使用了未使用的身份验证 身份。我只想连接数据库并通过 usermanager 插入用户。

听起来您想要一个不注册身份相关身份验证方案的纯粹解决方案

那么我们来看看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();
    }

您可以看到 AddIdentityCore 方法是您可能的解决方案

然后我们来看看AddIdentityCore方法里面的代码:

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

它在身份构建器中添加了大量服务,当构建 IServiceCollection 时,它会为您注册所需的服务,这就是为什么您会收到 DI 错误 尝试一下

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

现在,将 IAuthenticationSchemeProvider 注入 PageModel 并检查注册的 带有

_authenticationSchemeProvider.GetAllSchemesAsync()

的身份验证方案

当我尝试使用默认身份时,注册了 4 个方案:

当我尝试时

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

未注册任何身份验证方案,并在数据库中成功创建记录

© www.soinside.com 2019 - 2024. All rights reserved.