2017-10-10 53 views
0

我有asp.net 2.0核心的解決方案,它包含以下項目:ASP.NET核心Web API授權屬性返回404錯誤和重定向力

  • 數據:一個類庫,用於EF代碼
  • 的OAuth:如對於IdentityServer4代碼
  • API網絡應用項目:我的API爲空Web項目創建

現在OAuth的項目配置了aspnetidentity及其工作很好,我能得到令牌身份驗證後,這是它的啓動代碼:

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(); 
} 

現在的API項目的問題,每當我打開任何授權的控制器/動作的給我404錯誤,而這個啓動代碼:

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(); 
} 

如果我關閉上述這部分代碼:

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

然後授權屬性按預期方式工作,但另一個新的問題,它顯示500內部服務器錯誤,每當我運ened喜歡的AccountController任何控制器,它期待的UserManager的UserManager其構造

出現InvalidOperationException:無法解析 服務類型Microsoft.AspNetCore.Identity.UserManager`1 [MCare.Data.Entities.User] 試圖激活MCare.Api.Controllers.AccountsController

我想知道爲什麼發生這種情況,如何解決這個問題,而不與API項目混合IdentityServer4項目,因爲我已經看到了所有項目,他們在混合兩者一個項目。

+0

爲什麼在RestAPI項目中需要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和它沒有登錄頁面,我希望從該控制器返回401 UnAuthorized狀態代碼。 –

+0

在Core 2.0之前,有一個屬性可以關閉AutoChallenge。在2.0 Ithink中,您需要通過在選項中的OnRedirectToLogin事件中添加自己的代碼來覆蓋AutoChallenge。讓我看看我是否可以找到某個地方的文章... https://stackoverflow.com/questions/45878166/asp-net-core-2-0-disable-automatic-challenge – Geekn

+0

解決方案是顯式設置默認身份驗證並像你提到的那樣挑戰模式。謝謝。 –

相關問題