无法从根提供程序解析范围服务“Microsoft.AspNetCore.Identity.UserManager`1[IdentityServerSample.Models.ApplicationUser]”

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

我正在扩展身份服务器以使用自定义身份服务器实现

 public static void UseMongoDbForIdentityServer(this IApplicationBuilder app)
    {

        //Resolve Repository with ASP .NET Core DI help 
        var repository = (IRepository)app.ApplicationServices.GetService(typeof(IRepository));

        //Resolve ASP .NET Core Identity with DI help
        var userManager = (UserManager<ApplicationUser>)app.ApplicationServices.GetService(typeof(UserManager<ApplicationUser>));

        // --- Configure Classes to ignore Extra Elements (e.g. _Id) when deserializing ---
        ConfigureMongoDriver2IgnoreExtraElements();

        var createdNewRepository = false;

        ...
}

这就是我的启动文件的样子

      public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<ConfigurationOptions>(Configuration);
     ...

        services.AddIdentityServer(
                  options =>
                  {
                      options.Events.RaiseSuccessEvents = true;
                      options.Events.RaiseFailureEvents = true;
                      options.Events.RaiseErrorEvents = true;
                  }
              )
              .AddMongoRepository()
              .AddMongoDbForAspIdentity<ApplicationUser, IdentityRole>(Configuration)
              .AddClients()
              .AddIdentityApiResources()
              .AddPersistedGrants()
            .AddDeveloperSigningCredential();
      ...

    }

这就是我遇到的错误

Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteValidator.ValidateResolution(类型 服务类型,ServiceProvider 服务提供商) Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(类型 服务类型) IdentityServerSample.MongoDbStartup.UseMongoDbForIdentityServer(IApplicationBuilder 应用程序)在 MongoDbStartup.cs

   var userManager = (UserManager<ApplicationUser>)app.ApplicationServices.GetService(typeof(UserManager<ApplicationUser>));

IdentityServerSample.Startup.Configure(IApplicationBuilder应用程序, Startup.cs 中的 IHostingEnvironment env)

            app.UseMongoDbForIdentityServer();

System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() Microsoft.AspNetCore.Hosting.ConventionBasedStartup.Configure(IApplicationBuilder 应用程序) Microsoft.AspNetCore.ApplicationInsights.HostingStartup.ApplicationInsightsLoggerStartupFilter+<>c__DisplayClass0_0.b__0(IApplicationBuilder 建设者) Microsoft.ApplicationInsights.AspNetCore.ApplicationInsightsStartupFilter+<>c__DisplayClass0_0.b__0(IApplicationBuilder 应用程序) Microsoft.AspNetCore.Server.IISIntegration.IISSetupFilter+<>c__DisplayClass3_0.b__0(IApplicationBuilder 应用程序) Microsoft.AspNetCore.Hosting.Internal.AutoRequestServicesStartupFilter+<>c__DisplayClass0_0.b__0(IApplicationBuilder 建设者) Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()

我知道有人问类似的问题,但似乎没有一个能解决我的问题

c# asp.net-core asp.net-identity identityserver4
3个回答
45
投票

您需要一个作用域来解析注册为作用域的依赖项。要创建它,您可以使用以下命令:

using(var scope = app.ApplicationServices.CreateScope())
{
    //Resolve ASP .NET Core Identity with DI help
    var userManager = (UserManager<ApplicationUser>)scope.ServiceProvider.GetService(typeof(UserManager<ApplicationUser>));
    // do you things here
}

0
投票

如果您使用提供的 Asp netcore Identity (AppIdentityDbContext),如果您处于开发阶段,则可以尝试将 validateScope 更改为生产环境,在 Program.cs 中:

webBuilder.UseContentRoot(Directory.GetCurrentDirectory());
                    webBuilder.UseIISIntegration();
                    webBuilder.UseDefaultServiceProvider((context, options) =>
                    {
                        options.ValidateScopes = context.HostingEnvironment.IsProduction();
                    });

0
投票

我有同样的问题,但很容易解决,因为我只是忘记添加

.AddRoles<IdentityRole>()
也许有人也忘记了所以请记住

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