Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Asp.NETCore2.1Identity-UserStore依赖项注入_C#_Asp.net_.net_Asp.net Core_Asp.net Identity - Fatal编程技术网

C# Asp.NETCore2.1Identity-UserStore依赖项注入

C# Asp.NETCore2.1Identity-UserStore依赖项注入,c#,asp.net,.net,asp.net-core,asp.net-identity,C#,Asp.net,.net,Asp.net Core,Asp.net Identity,我正试图为我的身份模型实现一个定制的UserStore;但是,我在应用程序启动时遇到以下运行时错误: InvalidOperationException:无法解析类型的服务 “[Project].Models.Identity.ApplicationUserStore”时尝试 激活“[Project].Models.Identity.ApplicationUserManager” 堆栈: Microsoft.Extensions.DependencyInjection.ServiceLookup

我正试图为我的身份模型实现一个定制的
UserStore
;但是,我在应用程序启动时遇到以下运行时错误:

InvalidOperationException:无法解析类型的服务 “[Project].Models.Identity.ApplicationUserStore”时尝试 激活“[Project].Models.Identity.ApplicationUserManager”

堆栈:

Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(类型 serviceType,Type implementationType,CallSiteChain CallSiteChain, ParameterInfo[]参数,bool throwIfCallSiteNotFound) Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite(类型 serviceType,Type implementationType,CallSiteChain CallSiteChain) Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(ServiceDescriptor 描述符,类型serviceType,CallSiteChain CallSiteChain) Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(类型 服务类型,CallSiteChain(CallSiteChain) Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateCallSite(类型 服务类型,CallSiteChain(CallSiteChain) Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.CreateServiceAccessor(类型 serviceType)System.Collections.Concurrent.ConcurrentDictionary.GetOrAdd(TKey,Func valueFactory) Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(类型 serviceType,ServiceProviderEngineScope ServiceProviderEngineScope) Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(类型 服务类型) Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider 提供程序,类型(服务类型) Microsoft.AspNetCore.Identity.IdentityBuilder+c__显示类22_0.b__0(IServiceProvider服务) Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite,ServiceProviderEngineeScope范围) Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor.VisitCallSite(IServiceCallSite callSite,Target参数) Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite,ServiceProviderEngineeScope范围) Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor.VisitCallSite(IServiceCallSite callSite,Target参数) Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine+c\u DisplayClass1\u 0.b\u 0(ServiceProviderEngineScope 范围) Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(类型 serviceType,ServiceProviderEngineScope ServiceProviderEngineScope) Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(类型 服务类型) Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp,类型类型,类型要求者,布尔isDefaultParameterRequired) lambda_方法(闭包、IServiceProvider、对象[]) Microsoft.AspNetCore.Mvc.Controllers.ControllerActivatorProvider+c__DisplayClass4_0.b__0(ControllerContext 控制器上下文) Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider+c_uuDisplayClass5_0.g_uCreateController|0(ControllerContext 控制器上下文) Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(参考 状态next、ref范围、ref对象状态、ref bool已完成) Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync() Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter() Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext 上下文)Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(参考 状态next、ref范围、ref对象状态、ref bool已完成) Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync() Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync() Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext) Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext (上下文) Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext (上下文) Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext (上下文)

我的
UserStore
实现:

public class ApplicationUserStore : IUserStore<Employee>,
                     IUserClaimStore<Employee>,
                     IUserLoginStore<Employee>,
                     IUserRoleStore<Employee>,
                     IUserPasswordStore<Employee>,
                     IUserSecurityStampStore<Employee>
{
    #region Constructor signatures I tried for dependency injection
    public ApplicationUserStore(ApplicationDbContext context, IdentityErrorDescriber describer = null)
    {

    }
    public ApplicationUserStore(ApplicationDbContext context)
    {

    }
    public ApplicationUserStore()
    {

    }
    public ApplicationUserStore(DbContext context)
    {

    }
    #endregion
    ...
public class ApplicationUserManager : UserManager<Employee>
{
    public ApplicationUserManager(ApplicationUserStore store, IOptions<IdentityOptions> optionsAccessor, IPasswordHasher<Employee> passwordHasher, IEnumerable<IUserValidator<Employee>> userValidators, IEnumerable<IPasswordValidator<Employee>> passwordValidators, ILookupNormalizer keyNormalizer, IdentityErrorDescriber errors, IServiceProvider services, ILogger<UserManager<Employee>> logger)
        : base(store, optionsAccessor, passwordHasher, userValidators, passwordValidators, keyNormalizer, errors, services, logger)
    {
    }
}
public class ApplicationSignInManager : SignInManager<Employee>
{
    public ApplicationSignInManager(ApplicationUserManager userManager, IHttpContextAccessor contextAccessor, IUserClaimsPrincipalFactory<Employee> claimsFactory, IOptions<IdentityOptions> optionsAccessor, ILogger<SignInManager<Employee>> logger, IAuthenticationSchemeProvider schemes)
        : base(userManager, contextAccessor, claimsFactory, optionsAccessor, logger, schemes)
    {
    }
}
My
SignInManager
实现:

public class ApplicationUserStore : IUserStore<Employee>,
                     IUserClaimStore<Employee>,
                     IUserLoginStore<Employee>,
                     IUserRoleStore<Employee>,
                     IUserPasswordStore<Employee>,
                     IUserSecurityStampStore<Employee>
{
    #region Constructor signatures I tried for dependency injection
    public ApplicationUserStore(ApplicationDbContext context, IdentityErrorDescriber describer = null)
    {

    }
    public ApplicationUserStore(ApplicationDbContext context)
    {

    }
    public ApplicationUserStore()
    {

    }
    public ApplicationUserStore(DbContext context)
    {

    }
    #endregion
    ...
public class ApplicationUserManager : UserManager<Employee>
{
    public ApplicationUserManager(ApplicationUserStore store, IOptions<IdentityOptions> optionsAccessor, IPasswordHasher<Employee> passwordHasher, IEnumerable<IUserValidator<Employee>> userValidators, IEnumerable<IPasswordValidator<Employee>> passwordValidators, ILookupNormalizer keyNormalizer, IdentityErrorDescriber errors, IServiceProvider services, ILogger<UserManager<Employee>> logger)
        : base(store, optionsAccessor, passwordHasher, userValidators, passwordValidators, keyNormalizer, errors, services, logger)
    {
    }
}
public class ApplicationSignInManager : SignInManager<Employee>
{
    public ApplicationSignInManager(ApplicationUserManager userManager, IHttpContextAccessor contextAccessor, IUserClaimsPrincipalFactory<Employee> claimsFactory, IOptions<IdentityOptions> optionsAccessor, ILogger<SignInManager<Employee>> logger, IAuthenticationSchemeProvider schemes)
        : base(userManager, contextAccessor, claimsFactory, optionsAccessor, logger, schemes)
    {
    }
}

我需要知道问题的原因、解决方法,以及是否有Asp.Net Core 2.1依赖项注入激活器签名的文档。

在回顾了开源实现之后,我找到了问题的线索,我猜对了。
UserStore
需要通过该行在
Startup.ConfigureServices
中注册:

        ...
        services.AddIdentityCore<Employee>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddUserStore<ApplicationUserStore>()
            .AddUserManager<ApplicationUserManager>()
            .AddSignInManager<ApplicationSignInManager>()
            .AddDefaultTokenProviders();

        services.AddTransient<IUserStore<Employee>, ApplicationUserStore>();
        ...
    services.AddScoped<ApplicationUserStore>();
services.addScope();
因此,我的最终方法是:

    ...
    services.AddIdentityCore<Employee>()
        .AddUserManager<ApplicationUserManager>()
        .AddSignInManager<ApplicationSignInManager>()
        .AddDefaultTokenProviders();

    services.AddScoped<IUserStore<Employee>, ApplicationUserStore>();
    services.AddScoped<ApplicationUserStore>();
    ...
。。。
services.AddIdentityCore()
.AddUserManager()
.AddSignInManager()的名称
.AddDefaultTokenProviders();
services.addScope();
services.addScope();
...
尽管如此,它在没有:

    services.AddScoped<IUserStore<Employee>, ApplicationUserStore>();
services.addScope();

如果声明services.AddTransient(),则不能在构造函数中使用ApplicationUserStore。你需要使用IUserStore@hugo. 不幸的是,在没有
AddUserStore
的情况下使用
AddTransient
对我不起作用,在没有
AddTransient
的情况下使用
AddUserStore
也不起作用;我一直收到相同的消息。您注册了
ApplicationDbContext
?没有。该行只是告诉Identity要使用的IdentityDbContext的类型。它希望对其进行预配置和注册,例如
services.AddDbContext()