2017-09-09 69 views
1

错误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时会发生问题。

请有谁让我知道我该如何解决这个问题。

回答

2

在ASP.NET Core 2.0中,添加Jwt承载身份验证的方法已更改。请参阅您链接的示例的latest version

您需要将Jwt承载身份验证过程注册为服务,而不是中间件组件。见ConfigureServices相关代码:

services.AddAuthentication(...) 
    .AddJwtBearer(...); 

试试这个在ConfigureServices

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(); 

正如我不能对此进行测试反对你的情况,有一次它不会第一次工作的机会。当你包含任何来自此的更多信息时,请务必详细说明。

+0

能否请你告诉我addjwtbearer –

+0

我得到一个错误HTTP 500错误 这很奇怪......添加此行 –

+0

后,网站无法显示该页面的等效代码所添加的代码 - –