>我正在扩展身份服务器以使用自定义身份服务器实现
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(Type 服务类型,服务提供者服务提供者( Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type 服务类型( IdentityServerSample.MongoDbStartup.UseMongoDbForIdentityServer(IApplicationBuilder app(在MongoDbStartup中.cs
var userManager = (UserManager<ApplicationUser>)app.ApplicationServices.GetService(typeof(UserManager<ApplicationUser>));
IdentityServerSample.Startup.Configure(IApplicationBuilder app, IHostingEnvironment env(在启动.cs
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((
我知道有人问过类似的问题,但似乎没有一个能解决我的问题
需要一个作用域来解析注册为作用域的依赖项。要创建它,您可以使用以下内容:
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
}
如果您使用的是提供的 Asp netcore Identity (AppIdentityDbContext(,则可以尝试将 validateScope 更改为生产环境(如果您在开发中,在程序.cs中:
webBuilder.UseContentRoot(Directory.GetCurrentDirectory());
webBuilder.UseIISIntegration();
webBuilder.UseDefaultServiceProvider((context, options) =>
{
options.ValidateScopes = context.HostingEnvironment.IsProduction();
});