简体   繁体   English

Auth0 api 返回 401 www-authenticate: Bearer error="invalid_token" in .net core web api

[英]Auth0 api returning 401 www-authenticate: Bearer error="invalid_token" in .net core web api

I am using auth0 with .net core web api, below are my configuration.我将 auth0 与 .net core web api 一起使用,下面是我的配置。

  1. In my ConfigureServices() I have在我的 ConfigureServices() 我有

    services.AddAuthentication(options => { options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }).AddJwtBearer(options => { options.Authority = "https://xxxxxxx.auth0.com/"; options.Audience = "https://localhost:5001"; });
  2. In my Configure() method I have在我的 Configure() 方法中,我有

    // 2. Enable authentication middleware app.UseAuthentication(); app.UseMvc();

And finally in my HomeController.cs最后在我的 HomeController.cs

    [HttpGet("private")]
    [Authorize]
    public IActionResult Private()
    {
        return Ok(new
        {
            Message = "Hello from a private endpoint! You need to be authenticated to see this."
        });
    }

And when I try to access the endpoint, with the right access token, using postman or my react app I am getting 401 unauthorized or www-authenticate: Bearer error="invalid_token"当我尝试使用正确的访问令牌,使用邮递员或我的反应应用程序访问端点时,我收到 401 未授权或www-authenticate: Bearer error="invalid_token"

在此处输入图片说明

I followed the documentation for examples, cannot figure out what I am doing wrong here.我遵循了示例文档,无法弄清楚我在这里做错了什么。 Please advice.请指教。

Try specifying the scheme you use explicitly:尝试明确指定您使用的方案:

[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]

instead of代替

[Authorize]

Try this way with configuration of token validation通过配置令牌验证尝试这种方式

.AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, options =>
{
    options.TokenValidationParameters = new TokenValidationParameters
    {
        ValidateIssuer = true,
        ValidIssuer = "https://xxxxxxx.auth0.com/",
        ValidateAudience = true,
        ValidAudience = "https://localhost:5001",
        ValidateLifetime = true,
        IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(YourSecretKey)),
        ValidateIssuerSigningKey = true
    };
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JWT Bearer 不断返回 401 状态 - Bearer error="invalid_token", error_description="签名无效" - JWT Bearer Keeps returning 401 Status - Bearer error="invalid_token", error_description="The signature is invalid" 令牌未发送到 angular WWW-Authenticate: Bearer 中的服务器并获得 401 错误 - token not send to the server in angular WWW-Authenticate: Bearer and get 401 error .Net Core Web API的Auth0身份验证 - Auth0 Authentication for .Net Core Web API .Net core api with AD B2C OAuth 2.0 - Invalid_token 错误 - .Net core api with AD B2C OAuth 2.0 - Invalid_token error ASP.NET Core WebAPI: Bearer error="invalid_token", error_description="未找到签名密钥" - ASP.NET Core WebAPI: Bearer error="invalid_token", error_description="The signature key was not found" c# asp.net 核心承载错误="invalid_token" - c# asp.net core Bearer error="invalid_token" ASP.NET 核心 web api 编辑承载认证 - ASP.NET Core web api editing Bearer auth Error Bearer error="invalid_token" 使用 MSAL 验证令牌 - Error Bearer error="invalid_token" validate token with MSAL 不带重定向的承载令牌WEB API asp.net core - Bearer token WEB API asp.net core without redirection JWT不记名令牌授权不起作用asp net core web api - JWT bearer token Authorization not working asp net core web api
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM