將Scott Wildermuth的World Trip應用程序升級到ASP.NET Core 2.0。下面的代碼不起作用。Cookie身份驗證不適用於asp.net核心中的授權政策
由於我使用兩種認證類型,我會喜歡這兩種工作的API控制器,我決定使用授權策略。
public void ConfigureServices(IServiceCollection services)
{
//Some codes here
services.AddAuthentication()
.AddCookie()
.AddJwtBearer(**Implementation is fine**);
services.AddAuthorization(options =>
{
options.AddPolicy("Authenticated", policy =>
{
policy.AddAuthenticationSchemes(
CookieAuthenticationDefaults.AuthenticationScheme,
JwtBearerDefaults.AuthenticationScheme)
.RequireAuthenticatedUser();
});
});
}
在我的控制器
現在,
namespace TheWorld.Controllers.Api
{
[Route("api/trips")]
[Authorize(policy: "Authenticated")]
public class TripsController : controller
{
// Implementation is fine
}
}
請求從客戶端(網頁)來與cookie認證是從未見過的認證,同時從智威湯遜請求驗證客戶端正常工作。
如果我在控制器上使用簡單[Authorize]
,它只適用於cookie身份驗證,其中asp.net核心只選擇默認的cookie身份驗證,並且從不接受來自Jwt客戶端的請求。
謝謝@poke。我已經嘗試過你提到過的情況,在這種情況下發生的事情是'CookieAuthentication'被選爲默認值,'JwtBearer'從未被使用過。 謝謝。 –
謝謝@poke。我終於發現問題出自'AspNetCore.Identity'。隨附的默認'AuthenticationScheme'使用方案名「'Identity.Application」'。 因此,在將'CookieAuthenticationDefaults.AuthenticationScheme'改爲'「Identity.Application」後,應用程序工作,無論是否調用「services.AddAuthentication」上的'.AddCookie'。 –