C# JWTAuthentication在使用System.IdentityModel.Tokens.Jwt-5.1.4更新从1.1迁移到2.0后在asp.net core 2.0中不起作用
错误 错误CS0619“JwtBearerAppBuilderExtensions.UseJwtBearerAuthentication(IAApplicationBuilder,JwtBearerOptions)”已过时:“请参阅” 以下是JWT身份验证的代码C# JWTAuthentication在使用System.IdentityModel.Tokens.Jwt-5.1.4更新从1.1迁移到2.0后在asp.net core 2.0中不起作用,c#,asp.net,asp.net-mvc,asp.net-core,jwt,C#,Asp.net,Asp.net Mvc,Asp.net Core,Jwt,错误 错误CS0619“JwtBearerAppBuilderExtensions.UseJwtBearerAuthentication(IAApplicationBuilder,JwtBearerOptions)”已过时:“请参阅” 以下是JWT身份验证的代码 app.UseJwtBearerAuthentication(new JwtBearerOptions { TokenValidationParameters = new Toke
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 core 1.1 I中运行良好,刚刚从core 1.1迁移到core 2.0,并将System.IdentityModel.Tokens.Jwt(5.1.1)更新为(5.1.4)
配置服务
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());
});
rsacameters-keyParams=RsaKeyUtils.GetRandomKey();
key=新的RsaSecurityKey(keyParams);
tokenOptions=新的TokenAuthOptions()
{
观众=观众,
发行人=代币发行人,
SigningCredentials=新的SigningCredentials(密钥、SecurityAlgorithms.rsasa256Signature)
};
服务。AddSingleton(令牌选项);
services.AddAuthorization(auth=>
{
auth.AddPolicy(“持有人”,新授权PolicyBuilder()
.addAuthenticationScheme(JwtBearerDefaults.AuthenticationScheme)
.RequireAuthenticatedUser().Build());
});
我尝试了此问题中发布的解决方案。但该解决方案不起作用,因为IServiceCollection不包含AddJWTBeareAuthentication的定义
试图实施
500内部服务器错误
同样的代码在asp.net core 1.1上运行得很好。当升级到2.0时,问题就出现了
请告诉我如何解决此问题。在ASP.NET Core 2.0中添加Jwt承载身份验证的方法已更改。请参阅您链接的示例的 您需要将Jwt承载身份验证过程注册为服务,而不是中间件组件。请参阅
ConfigureServices
中的相关代码:
services.AddAuthentication(...)
.AddJwtBearer(...);
services.AddAuthentication()
.AddJwtBearer(jwt =>
{
jwt.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)
};
});
services.AddAuthorization(auth =>
{
auth.AddPolicy("Bearer", new AuthorizationPolicyBuilder(JwtBearerDefaults.AuthenticationScheme)
.RequireAuthenticatedUser()
.Build());
});
在ConfigureServices
中尝试此操作:
services.AddAuthentication(...)
.AddJwtBearer(...);
services.AddAuthentication()
.AddJwtBearer(jwt =>
{
jwt.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)
};
});
services.AddAuthorization(auth =>
{
auth.AddPolicy("Bearer", new AuthorizationPolicyBuilder(JwtBearerDefaults.AuthenticationScheme)
.RequireAuthenticatedUser()
.Build());
});
并确保在Configure
中有此选项:
app.UseAuthentication();
由于我无法针对您的场景对此进行测试,因此很有可能第一次就无法使用。请确保在包含来自此场景的任何进一步信息时具体说明。让我们在聊天室中继续此讨论:。