Reputation: 2049
i am extending the identity server to use custom identity server implementation
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;
...
}
this is how my startup file looks like
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();
...
}
And this is the error i am getting
Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteValidator.ValidateResolution(Type serviceType, ServiceProvider serviceProvider) Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType) IdentityServerSample.MongoDbStartup.UseMongoDbForIdentityServer(IApplicationBuilder app) in MongoDbStartup.cs
var userManager = (UserManager<ApplicationUser>)app.ApplicationServices.GetService(typeof(UserManager<ApplicationUser>));
IdentityServerSample.Startup.Configure(IApplicationBuilder app, IHostingEnvironment env) in Startup.cs
app.UseMongoDbForIdentityServer();
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() Microsoft.AspNetCore.Hosting.ConventionBasedStartup.Configure(IApplicationBuilder app) Microsoft.AspNetCore.ApplicationInsights.HostingStartup.ApplicationInsightsLoggerStartupFilter+<>c__DisplayClass0_0.b__0(IApplicationBuilder builder) Microsoft.ApplicationInsights.AspNetCore.ApplicationInsightsStartupFilter+<>c__DisplayClass0_0.b__0(IApplicationBuilder app) Microsoft.AspNetCore.Server.IISIntegration.IISSetupFilter+<>c__DisplayClass3_0.b__0(IApplicationBuilder app) Microsoft.AspNetCore.Hosting.Internal.AutoRequestServicesStartupFilter+<>c__DisplayClass0_0.b__0(IApplicationBuilder builder) Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()
I know there are similar questions asked, but none seems to solve my problem
Upvotes: 16
Views: 11020
Reputation: 1
I had the same problem, but it was easy to solve because I just forgot to add
.AddRoles<IdentityRole>()
Maybe somebody also forgot so keep in mind
Upvotes: 0
Reputation: 1
If you're using provided Asp netcore Identity (AppIdentityDbContext), you can try changing validateScope to production if you were in Development, in Program.cs:
webBuilder.UseContentRoot(Directory.GetCurrentDirectory());
webBuilder.UseIISIntegration();
webBuilder.UseDefaultServiceProvider((context, options) =>
{
options.ValidateScopes = context.HostingEnvironment.IsProduction();
});
Upvotes: 0
Reputation: 9195
You need a scope to resolve dependencies registered as scoped. To create it you can use the following:
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
}
Upvotes: 49