2017-10-10 5 views
0

をリダイレクト:ASP.NETコアウェブAPI承認404エラー&力を返す属性私は次のプロジェクトが含まれているasp.netコア2.0ソリューション持っ

  • データ:EFコードのためのクラスライブラリ
  • のOAuth:としてのIdentityServer4コード
  • APIのWebアプリケーションプロジェクトは:私のAPIは空のWebプロジェクトとして作成されたため

は今のOAuthプロジェクトがaspnetidentityで構成され、そのトークンを取得する私ができるよう、細かい作業します認証後、これはそれのためのスタートアップコードです:今、APIプロジェクトに関する問題、私は任意の認可コントローラ/アクションを開くたびにその私に404エラーを与えており、このスタートアップコード

public void ConfigureServices(IServiceCollection services) 
{ 
    // connect with normal database 
    services.AddDbContext<MCareContext>(options => options.UseSqlServer(Configuration 
       .GetConnectionString("MCareConnection"))); 

    services.AddIdentity<User, IdentityRole>() 
     .AddEntityFrameworkStores<MCareContext>() 
     .AddDefaultTokenProviders(); 


    services.AddMvc(); 

    // IS configurations database 
    var dbConnectionString = Configuration.GetConnectionString("MCareConnection.OAuth"); 
    var migrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name; 

    services.AddIdentityServer() 
     .AddConfigurationStore(options => 
     { 
      options.ConfigureDbContext = builder => 
       builder.UseSqlServer(dbConnectionString, 
        sql => sql.MigrationsAssembly(migrationsAssembly)); 
     }) 

     .AddOperationalStore(options => 
     { 
      options.ConfigureDbContext = builder => 
       builder.UseSqlServer(dbConnectionString, 
        sql => sql.MigrationsAssembly(migrationsAssembly)); 
     }) 

     .AddAspNetIdentity<User>() 
     .AddDeveloperSigningCredential(); 
} 

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
{ 
    DatabaseInitializer.InitializeDatabase(app); 

    loggerFactory.AddConsole(); 

    if (env.IsDevelopment()) 
    { 
     app.UseDeveloperExceptionPage(); 
    } 

    app.UseIdentityServer(); 

    app.UseStaticFiles(); 
    app.UseMvcWithDefaultRoute(); 
} 

public void ConfigureServices(IServiceCollection services) 
{ 
    services.AddDbContext<MCareContext>(options => options.UseSqlServer(Configuration 
     .GetConnectionString("MCareConnection"))); 

    services.AddIdentity<User, IdentityRole>() 
     .AddEntityFrameworkStores<MCareContext>() 
     .AddDefaultTokenProviders(); 

    services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme) 
    .AddIdentityServerAuthentication(options => 
    { 
     options.Authority = "http://localhost:60415"; 
     options.ApiName = "mCareApi"; 
     options.RequireHttpsMetadata = false; 
    }); 

    services.AddMvc(); 
} 

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
{ 
    loggerFactory.AddConsole(); 

    app.UseAuthentication(); 

    if (env.IsDevelopment()) 
    { 
     app.UseDeveloperExceptionPage(); 
    } 

    app.UseDefaultFiles(); 
    app.UseStaticFiles();    

    app.UseMvcWithDefaultRoute(); 
} 

私は上記のコードでこの部分を閉じる場合:いつでもI OP

//services.AddIdentity<User, IdentityRole>() 
    //.AddEntityFrameworkStores<MCareContext>() 
    //.AddDefaultTokenProviders(); 

次に承認が期待通りに仕事を属性が、別の新しい問題、それは、500内部サーバーエラーを示しますそのコンストラクタ

InvalidOperationExceptionが上のUserManagerのUserManagerを期待AccountControllerのような任意のコントローラened:タイプ Microsoft.AspNetCore.Identity.UserManager`1 [MCare.Data.Entities.User]のためのサービスを解決することができませんを アクティブにしようとしたときMCare.Api.Controllers.AccountsController

私が見てきたすべてのプロジェクトは、彼らがそれらの両方を混ぜるので、私は、これはAPIプロジェクトでIdentityServer4プロジェクトを混合せず、起こって、どのようにこの問題を解決することを目的とする理由を知りたいです1つのプロジェクト。

+0

なぜRestPIプロジェクトにAspNetIdentityが必要ですか? – regnauld

+0

アカウントの作成、パスワードのリセット、およびその他のアカウント管理機能 –

+0

あなたのブラウザで許可されたルートを要求する瞬間からログを投稿してください。 – poke

答えて

5

DefaultChallengeSchemeが404の原因となるauthorize属性に遭遇したときにログインページなどの存在しないページにリダイレクトされる可能性はありますか?

デフォルトチャレンジを許可されていないJwtスキーマに設定してみてください。

services.AddAuthentication(options => 
{ 
    options.DefaultAuthenticateScheme = IdentityServerAuthenticationDefaults.AuthenticationScheme; 
    options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; 
}) 
.AddIdentityServerAuthentication(options => 
{ 
    options.Authority = "http://localhost:60415"; 
    options.ApiName = "mCareApi"; 
    options.RequireHttpsMetadata = false; 
}); 

また、イベントのハンドラを提供することで、以下の記事で説明した方法を試すこともできます。

services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme) 
.AddIdentityServerAuthentication(options => 
{ 
    options.JwtBearerEvents = new JwtBearerEvents 
    { 
     OnChallenge = context => 
     { 
      context.Response.StatusCode = 401; 
      return Task.CompletedTask; 
     } 
    }; 
    options.Authority = "http://localhost:60415"; 
    options.ApiName = "mCareApi"; 
    options.RequireHttpsMetadata = false; 
}); 
+0

はい、私はこのリダイレクトをWeb Apiとそのページにログインページがないために発生させたくありません。そのコントローラからUnauthorized状態コード401を返すことを期待しています。 –

+0

Core 2.0以前は、AutoChallengeを無効にするプロパティがありました。 2.0では、オプションのOnRedirectToLoginのイベントに独自のコードを追加することでAutoChallengeをオーバーライドする必要があります。その記事をどこかで見つけることができるかどうかを見てみましょう... https://stackoverflow.com/questions/45878166/asp-net-core-2-0-disable-automatic-challenge – Geekn

+0

解決策は、明示的に両方のデフォルト認証を設定することですあなたが言及したように挑戦スキーマ。ありがとう。 –

関連する問題