【发布时间】:2018-01-28 09:57:32
【问题描述】:
我正在尝试将我的旧身份验证转换为 .NET 2.0。我有以下代码:
app.UseJwtBearerAuthentication(new JwtBearerOptions
{
AutomaticAuthenticate = true,
IncludeErrorDetails = true,
Authority = "https://securetoken.google.com/xxxxx",
TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidIssuer = "https://securetoken.google.com/xxxxxx",
ValidateAudience = true,
ValidAudience = "xxxx",
ValidateLifetime = true,
},
});
我的新代码如下:
public void Configure(...)
{
...
app.UseAuthentication();
...
}
public void ConfigureServices(IServiceCollection services)
{
...
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.RequireHttpsMetadata = false;
options.IncludeErrorDetails = true;
options.Authority = "https://securetoken.google.com/xxxxxx";
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidIssuer = "https://securetoken.google.com/xxxxx",
ValidateAudience = true,
ValidAudience = "xxxxxx",
ValidateLifetime = true,
};
});
...
services.AddMvc();
services.AddAuthorization(......);
}
但在 2.0 中,我收到 404 响应。如果我从端点中删除 [Authorize] 属性,它会起作用。我的输出窗口显示如下:
Microsoft.AspNetCore.Hosting.Internal.WebHost:信息:请求 开始 HTTP/1.1 GET http://localhost:62423/api/users/info
Microsoft.AspNetCore.Authorization.DefaultAuthorizationService:信息: 用户授权失败:(空)。 Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:信息: 过滤器请求授权失败 'Microsoft.AspNetCore.Mvc.Authorization.AuthorizeFilter'。 Microsoft.AspNetCore.Mvc.ChallengeResult:信息:正在执行 带有身份验证方案的 ChallengeResult ()。Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationHandler:信息: AuthenticationScheme:Identity.Application 受到质疑。 Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:信息: 执行的动作 SORTE.API.ContentManager.Controllers.UsersController.Info (SORTE.API.ContentManager) 在 24.0837 毫秒内 Microsoft.AspNetCore.Hosting.Internal.WebHost:信息:请求 在 35.2446 毫秒内完成 302 Microsoft.AspNetCore.Hosting.Internal.WebHost:信息:请求 开始 HTTP/1.1 GET http://localhost:62423/Account/Login?ReturnUrl=%2Fapi%2Fusers%2Finfo
Microsoft.AspNetCore.Hosting.Internal.WebHost:信息:请求 5.8149ms 404 完成
从日志错误来看,它似乎试图将我重定向到/Account/Login,但我没有这样的端点,我的项目是一个 Web API。
我缺少一些配置吗?
【问题讨论】:
标签: authentication asp.net-core firebase-authentication