首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Openiddict无法解析OpenIddict.Core.IOpenIddictApplicationStore`类型的服务

Openiddict无法解析OpenIddict.Core.IOpenIddictApplicationStore`类型的服务
EN

Stack Overflow用户
提问于 2017-04-21 07:38:34
回答 1查看 899关注 0票数 1

我试图使用identity + jwt授权来设置.net核心项目,但我得到了以下错误:

'OpenIddict.Core.IOpenIddictApplicationStore1[OpenIddict.Models.OpenIddictApplication]' while attempting to activate 'OpenIddict.Core.OpenIddictApplicationManager1OpenIddict.Models.OpenIddictApplication'.类型的System.InvalidOperationException:无法解析服务

这是我的配置:

代码语言:javascript
复制
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();
    }
}

当我试图调用/api/authenticate/token路径时,会出现错误。有人能帮我解决这个问题吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-21 09:02:54

Yo需要注册实体框架存储。注册AddEntityFrameworkCoreStores服务时添加对OpenIddict的调用:

代码语言:javascript
复制
 services.AddOpenIddict()
     .AddEntityFrameworkCoreStores<ApplicationDbContext>()
     ...

顺便说一下,考虑使用扩展版本的AddOpenIddict方法(检查openiddict 示例)

代码语言:javascript
复制
services.AddOpenIddict(options =>
            {
                 options.DisableHttpsRequirement();

                 options.AllowAuthorizationCodeFlow()
                       .AllowPasswordFlow()
                       .AllowRefreshTokenFlow();
                 ...
            }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43536709

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档