Asp.net core Openiddict无法解析类型为';OpenIddict.Core.iopenidictapplicationstore`
我试图使用identity+jwt授权设置.net core项目,但出现以下错误: System.InvalidOperationException:在尝试激活“OpenIddict.Core.OpenIddictApplicationManager1[OpenIddict.Models.OpenIddictApplication]”时,无法解析类型为“OpenIddict.Core.IOpenIddictApplicationStoreAsp.net core Openiddict无法解析类型为';OpenIddict.Core.iopenidictapplicationstore`,asp.net-core,openiddict,Asp.net Core,Openiddict,我试图使用identity+jwt授权设置.net core项目,但出现以下错误: System.InvalidOperationException:在尝试激活“OpenIddict.Core.OpenIddictApplicationManager1[OpenIddict.Models.OpenIddictApplication]”时,无法解析类型为“OpenIddict.Core.IOpenIddictApplicationStore1[OpenIddict.Models.OpenIddic
1[OpenIddict.Models.OpenIddictApplication]”的服务
以下是我的配置:
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();
}
}
公共类启动
{
公共启动(IHostingEnvironment环境)
{
var builder=new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile(“appsettings.json”,可选:true,重载更改:true)
.AddJsonFile($“appsettings.{env.EnvironmentName}.json”,可选:true)
.AddenEnvironmentVariables();
Configuration=builder.Build();
}
公共IConfigurationRoot配置{get;}
//此方法由运行时调用。请使用此方法将服务添加到容器中。
public void配置服务(IServiceCollection服务)
{
//添加框架服务。
services.AddMvc();
services.AddEntityFrameworkSqlServer()
.AddDbContext(选项=>{
使用SQLServer(配置[“数据:默认连接:连接字符串]);
options.UseOpenIddict();
});
//添加标识
服务.额外性()
.AddEntityFrameworkStores()
.AddDefaultTokenProviders();
//添加OpenIddict
services.AddOpenIddict()
.DisableHttpsRequirement()
.EnableTokenEndpoint(“/api/authenticate/token”)
.AllowPasswordFlow()
.AllowRefreshTokenFlow()
.UseJsonWebTokens()的
.AddEphemeralSigningKey();
services.AddTransient();
}
//此方法由运行时调用。请使用此方法配置HTTP请求管道。
public void Configure(IApplicationBuilder应用程序、IHostingEnvironment环境、iLogger工厂、IDatabaseInitializer初始化器)
{
loggerFactory.AddConsole(Configuration.GetSection(“Logging”);
loggerFactory.AddDebug();
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseWebpackDevMiddleware(新的WebpackDevMiddlewareOptions{
HotModuleReplacement=true
});
}
其他的
{
app.UseExceptionHandler(“/Home/Error”);
}
app.UseStaticFiles();
app.UseOpenIddict();
//使用jwt承载身份验证
应用程序UseJWTBeareAuthentication(新JWTBeareOptions
{
自动验证=真,
自动挑战=正确,
RequireHttpsMetadata=false,
观众=”http://localhost:1804/",
权威=”http://localhost:1804/"
});
app.UseMvc(路由=>
{
routes.MapRoute(
名称:“默认”,
模板:“{controller=Home}/{action=Index}/{id?}”);
routes.MapSpaFallbackRoute(
名称:“水疗后援”,
默认值:新建{controller=“Home”,action=“Index”});
});
初始值设定项Seed();
}
}
尝试调用/api/authenticate/token
路径时出现错误。
有人能帮我解决这个问题吗?您需要注册实体框架存储。注册OpenIddict服务时添加调用AddEntityFrameworkCoreStores
:
services.AddOpenIddict()
.AddEntityFrameworkCoreStores<ApplicationDbContext>()
...
services.AddOpenIddict(options =>
{
options.DisableHttpsRequirement();
options.AllowAuthorizationCodeFlow()
.AllowPasswordFlow()
.AllowRefreshTokenFlow();
...
}