2017-09-09 51 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

後,網站無法顯示該頁面的等效代碼所添加的代碼 - –