Reputation: 1165
I'm trying to setup the .net core project using identity + jwt authorization, but I'm getting the following error:
System.InvalidOperationException: Unable to resolve service for type 'OpenIddict.Core.IOpenIddictApplicationStore
1[OpenIddict.Models.OpenIddictApplication]' while attempting to activate 'OpenIddict.Core.OpenIddictApplicationManager
1[OpenIddict.Models.OpenIddictApplication]'.
Here is my configuration:
public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();
services.AddEntityFrameworkSqlServer()
.AddDbContext<ApplicationDbContext>(options => {
options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]);
options.UseOpenIddict();
});
// add identity
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
// add OpenIddict
services.AddOpenIddict()
.DisableHttpsRequirement()
.EnableTokenEndpoint("/api/authenticate/token")
.AllowPasswordFlow()
.AllowRefreshTokenFlow()
.UseJsonWebTokens()
.AddEphemeralSigningKey();
services.AddTransient<IDatabaseInitializer, DatabaseInitializer>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IDatabaseInitializer initializer)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions {
HotModuleReplacement = true
});
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseOpenIddict();
// use jwt bearer authentication
app.UseJwtBearerAuthentication(new JwtBearerOptions
{
AutomaticAuthenticate = true,
AutomaticChallenge = true,
RequireHttpsMetadata = false,
Audience = "http://localhost:1804/",
Authority = "http://localhost:1804/"
});
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
routes.MapSpaFallbackRoute(
name: "spa-fallback",
defaults: new { controller = "Home", action = "Index" });
});
initializer.Seed();
}
}
The error appears when I try to call the /api/authenticate/token
path.
Could anyone help me in resolving that issue?
Upvotes: 2
Views: 1677
Reputation: 49779
Yo need to register the Entity Framework stores. Add calling the AddEntityFrameworkCoreStores
when you register the OpenIddict services:
services.AddOpenIddict()
.AddEntityFrameworkCoreStores<ApplicationDbContext>()
...
By the way, consider using the extended version of AddOpenIddict
method (check openiddict sample)
services.AddOpenIddict(options =>
{
options.DisableHttpsRequirement();
options.AllowAuthorizationCodeFlow()
.AllowPasswordFlow()
.AllowRefreshTokenFlow();
...
}
Upvotes: 3