.NET Core IssuerSigningKey from file for JWT Bearer Authentication

前端 未结 2 1214
我在风中等你
我在风中等你 2020-12-14 02:45

I am struggling with the implementation (or the understanding) of signing keys for JWT Bearer Token authentication. And I hope somebody can help me or explain me what I am m

2条回答
  •  醉话见心
    2020-12-14 03:08

    Oh dear, that simple:

    SecurityKey key = new X509SecurityKey(cert);
    

    Or as complete sample from above:

    X509Certificate2 cert = new X509Certificate2("MySelfSignedCertificate.pfx", "password");
    SecurityKey key = new X509SecurityKey(cert); //well, seems to be that simple
    app.UseJwtBearerAuthentication(new JwtBearerOptions
    {
        AutomaticAuthenticate = true,
        AutomaticChallenge = true,
        TokenValidationParameters = new TokenValidationParameters
        {
            ValidateIssuer = true,
            ValidIssuer = "MyIssuer",
            ValidateAudience = true,
            ValidAudience = "MyAudience",
            ValidateLifetime = true,
            IssuerSigningKey = key
         }
    });
    

提交回复
热议问题