錯誤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錯誤 這很奇怪......添加此行 –
後,網站無法顯示該頁面的等效代碼所添加的代碼 - –