错误JWTAuthentication未在asp.net核心2.0加工后迁移1.1至2.0与System.IdentityModel.Tokens.Jwt - 5.1.4更新
错误CS0619 'JwtBearerAppBuilderExtensions.UseJwtBearerAuthentication(IApplicationBuilder,JwtBearerOptions)' 已过时:“看https://go.microsoft.com/fwlink/?linkid=845470”
下面是JWT认证
app.UseJwtBearerAuthentication(new JwtBearerOptions
{
TokenValidationParameters = new TokenValidationParameters
{
IssuerSigningKey = key,
ValidAudience = tokenOptions.Audience,
ValidIssuer = tokenOptions.Issuer,
// When receiving a token, check that it is still valid.
ValidateLifetime = true,
// This defines the maximum allowable clock skew - i.e. provides a tolerance on the token expiry time
// when validating the lifetime. As we're creating the tokens locally and validating them on the same
// machines which should have synchronised time, this can be set to zero. Where external tokens are
// used, some leeway here could be useful.
ClockSkew = TimeSpan.FromMinutes(0)
}
});
相同的代码在asp.net 1.1核心工作我精的代码,刚刚从核心1.1迁移到核心2.0及更新System.I dentityModel.Tokens.Jwt(5.1.1)到(5.1.4)
ConfigureServices
RSAParameters keyParams = RsaKeyUtils.GetRandomKey();
key = new RsaSecurityKey(keyParams);
tokenOptions = new TokenAuthOptions()
{
Audience = TokenAudience,
Issuer = TokenIssuer,
SigningCredentials = new SigningCredentials(key, SecurityAlgorithms.RsaSha256Signature)
};
services.AddSingleton<TokenAuthOptions>(tokenOptions);
services.AddAuthorization(auth =>
{
auth.AddPolicy("Bearer", new AuthorizationPolicyBuilder()
.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
.RequireAuthenticatedUser().Build());
});
我试过张贴在这个问题Authentication in dot net core preview-2.0解决方案。但解决的办法是行不通的得到一个错误的IServiceCollection不包含定义AddJwtBearerAuthentication
试图执行https://github.com/aspnet/Security/blob/c5b566ed4abffac4cd7011e0465e012cf503c871/samples/JwtBearerSample/Startup.cs#L47-L53 https://github.com/IdentityServer/IdentityServer4/issues/1055 https://blogs.msdn.microsoft.com/webdev/2017/04/06/jwt-validation-and-authorization-in-asp-net-core/
500内部服务器错误。
相同的代码在asp.net核心1.1上完美工作。当升级到2.0时会发生问题。
请有谁让我知道我该如何解决这个问题。
能否请你告诉我addjwtbearer –
我得到一个错误HTTP 500错误 这很奇怪......添加此行 –
后,网站无法显示该页面的等效代码所添加的代码 - –