2017-04-21 8 views
1

私はアイデンティティ+ JWTの認可を使用してセットアップには、.NETのコアプロジェクトをしようとしているが、私は次のエラーを取得しています:Openiddict「OpenIddict.Core.IOpenIddictApplicationStore`

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]'.

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パスを呼び出そうとするとエラーが表示されます。ここに私の構成です。 誰でも問題の解決に手伝ってもらえますか?

答えて

2

エンティティフレームワークストアを登録する必要があります。あなたはOpenIddictサービスを登録するときAddEntityFrameworkCoreStoresを呼び出す追加:

services.AddOpenIddict() 
    .AddEntityFrameworkCoreStores<ApplicationDbContext>() 
    ... 

ところで、AddOpenIddict方法の拡張版を使用することを検討してください(openiddict sampleをご確認ください)

services.AddOpenIddict(options => 
      { 
       options.DisableHttpsRequirement(); 

       options.AllowAuthorizationCodeFlow() 
         .AllowPasswordFlow() 
         .AllowRefreshTokenFlow(); 
       ... 
      } 
関連する問題