【发布时间】:2018-02-20 21:50:10
【问题描述】:
将 Scott Wildermuth 的 World Trip 应用程序升级到 ASP.NET Core 2.0。下面的代码不起作用。
由于我使用两种身份验证类型并且我希望两者都在 api 控制器上工作,因此我决定使用授权策略。
public void ConfigureServices(IServiceCollection services)
{
//Some code 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 身份验证的客户端(Web)的请求永远不会被视为已通过身份验证,而来自 Jwt 身份验证客户端的请求按预期工作。
如果我在控制器上使用简单的[Authorize],它只适用于 cookie 身份验证,其中 asp.net 核心只选择默认的 cookie 身份验证,并且从不接受来自 Jwt 客户端的请求。
【问题讨论】:
标签: c# asp.net-mvc authentication cookies asp.net-core