Docker JWT中的C#.net核心授权错误401未经授权

时间:2018-06-06 08:13:51

标签: c# .net-core jwt bearer-token

需要通过JWT Bearer确保在docker中授权.net Core web api

在我的Startup.cs中:

...
public void ConfigureServices(IServiceCollection services) {
  var pem = File.ReadAllText("publickey.pem");
  byte[] bytes = Encoding.ASCII.GetBytes(pem);

  services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
    .AddJwtBearer(options => {
      options.TokenValidationParameters = new TokenValidationParameters {
        ValidateLifetime = true,
        ValidateIssuerSigningKey = true,
        IssuerSigningKey = new SymmetricSecurityKey(bytes)
      };
    });

    services.AddMvc();
  }

  public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
    if (env.IsDevelopment()) {
      app.UseDeveloperExceptionPage();
    }

    app.UseAuthentication();
    app.UseMvc();
  }
...

我的publickey.pem看起来像这样:

-----BEGIN PUBLIC KEY-----
JBkjgkVBJKlgkljvk.jgk;4jkgJLBjkgJHcvbnVBcCghfb+dfdFdf5fdf54ASfG5
Sov1MTHDHZHORsZgWQKTbmfv/BjsgdfbvKJGuklVhjklTuiltgjkgtJKLgJKLTUG
diGauBf4HfFZP/iJZq43cuPXKXRRhX8Xp/3FGZ1HL4FRiXdAnoPuY/ofBclXEIUM
XWpJcBdN4pUiV2sCFDDMx6+KDl69orsfIwHnX3trjIeSJX7RWJyBOI8A1PJmeYFJ
T9Paq5qFdxxF6udNoxhZN5kTTmCqZTLesQf3mk1HLj759ZS8E1NlxoXWS3h8D3jd
SHesaEltPlrdA/y3rRa84OrwyryLp9kh8IOYXIifXPyeP1RWP2bGDkBcHiSyzgr/
cwIDAQAB
-----END PUBLIC KEY-----

我的控制员:

[Route("check")]
public class CheckController : ReqController {
  [HttpGet, Authorize]
  public Response Task() {
    try {
      token = Request.Headers["Authorization"];
      DB.Check();
    } catch (Exception e) {
      response.Status = e.Message;
    }
    return response;
  }
}

通过Postman致电我的api: 我选择了GET方法。在Authorization标签上选择Bearer Token并插入我的令牌

在响应标头中发送请求后,我收到:

Content-Length →0
Date →Wed, 06 Jun 2018 08:01:02 GMT
Server →Kestrel
WWW-Authenticate →Bearer error="invalid_token", error_description="The signature is invalid"

在网站https://jwt.io/我还检查了我的令牌(RSASHA256)和我的公钥,并向我显示Signature Verified

我可能犯错的地方或错过的地方?

0 个答案:

没有答案