【问题标题】:Migrating web api authentication from .NET Core 1.1 to 2.0将 Web api 身份验证从 .NET Core 1.1 迁移到 2.0
【发布时间】: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


    【解决方案1】:

    我遇到了同样的问题,直到我读到this

    当我们使用Authorize属性时,它实际上是默认绑定到第一个认证系统的。

    解决方案是指定要使用的方案 (JwtBearer):

    [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme, Policy = "PoliceName")]
    

    现在我可以获得状态 200(使用有效令牌)和 401(未经授权 - 无效令牌)

    【讨论】:

    • 谢谢!我被困在这个问题上将近一个星期。现在我的索赔变压器不工作,但这是另一个问题......
    • 哇。哇。谢谢。
    • 也非常感谢,自从更新到 asp.net core 2.0 后遇到同样的问题!
    猜你喜欢
    • 2018-02-15
    • 2018-04-05
    • 2018-01-27
    • 1970-01-01
    • 2020-10-15
    • 1970-01-01
    • 2018-06-12
    • 2018-06-23
    • 1970-01-01
    相关资源
    最近更新 更多