2017-08-12 1 views
0

Ich arbeite mit einem Token-basierten System zum ersten Mal in asp.net Core. Ich habe diesen Artikel folgt:.net Kern JwtBearerAuthentication nicht definiert

https://stormpath.com/blog/token-authentication-asp-net-core

Mein startup.cs sieht wie folgt aus:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Threading.Tasks; 
using blog.Persistence; 
using Microsoft.AspNetCore.Builder; 
using Microsoft.AspNetCore.Hosting; 
using Microsoft.AspNetCore.SpaServices.Webpack; 
using Microsoft.Extensions.Configuration; 
using Microsoft.Extensions.DependencyInjection; 
using Microsoft.Extensions.Logging; 
using Microsoft.EntityFrameworkCore; 
using AutoMapper; 
using System.Text; 
using Microsoft.IdentityModel.Tokens; 

namespace WebApplicationBasic 
{ 
    public class Startup 
    { 
     public Startup(IHostingEnvironment env) 
     { 
      var builder = new ConfigurationBuilder() 
       .SetBasePath(env.ContentRootPath) 
       .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) 
       .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) 
       .AddEnvironmentVariables(); 
      Configuration = builder.Build(); 
     } 

     public IConfigurationRoot Configuration { get; } 

     // This method gets called by the runtime. Use this method to add services to the container. 
     public void ConfigureServices(IServiceCollection services) 
     { 
      services.AddAutoMapper(); 

      services.AddDbContext<VegaDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("Default"))); 

      // Add framework services. 
      services.AddMvc(); 
     } 

     // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 
     public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
     { 
      // secretKey contains a secret passphrase only your server knows 
      var signingKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(Configuration.GetSection("TokenAuthentication:SecretKey").Value)); 

      var tokenValidationParameters = new TokenValidationParameters 
      { 
       // The signing key must match! 
       ValidateIssuerSigningKey = true, 
       IssuerSigningKey = signingKey, 

       // Validate the JWT Issuer (iss) claim 
       ValidateIssuer = true, 
       ValidIssuer = "ExampleIssuer", 

       // Validate the JWT Audience (aud) claim 
       ValidateAudience = true, 
       ValidAudience = "ExampleAudience", 

       // Validate the token expiry 
       ValidateLifetime = true, 

       // If you want to allow a certain amount of clock drift, set that here: 
       ClockSkew = TimeSpan.Zero 
      }; 

      app.UseJwtBearerAuthentication(new JwtBearerOptions 
      { 
       AutomaticAuthenticate = true, 
       AutomaticChallenge = true, 
       TokenValidationParameters = tokenValidationParameters 
      }); 

      loggerFactory.AddConsole(Configuration.GetSection("Logging")); 
      loggerFactory.AddDebug(); 

      if (env.IsDevelopment()) 
      { 
       app.UseDeveloperExceptionPage(); 
       app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions { 
        HotModuleReplacement = true 
       }); 
      } 
      else 
      { 
       app.UseExceptionHandler("/Home/Error"); 
      } 

      app.UseStaticFiles(); 

      app.UseMvc(routes => 
      { 
       routes.MapRoute(
        name: "default", 
        template: "{controller=Home}/{action=Index}/{id?}"); 

       routes.MapSpaFallbackRoute(
        name: "spa-fallback", 
        defaults: new { controller = "Home", action = "Index" }); 
      }); 
     } 
    } 
} 

Das Problem ist, dass UseJwtBearerAuthentication und JwtBearerOptions nicht definiert sind. Was könnte falsch sein? Muss ich noch ein anderes Paket bekommen?

+0

Haben Sie einen Verweis auf das richtige Paket hinzugefügt ?? – Tseng

Antwort

0

Nach @ Tsengs Kommentar, ich heruntergeladen den Referenzcode von Github und entfernt das Microsoft.AspNetCore.Authentication.JwtBearer-Paket, sicher genug, ich habe den Fehler "Der Typ oder Namespace-Name konnte nicht gefunden werden" für beide UseJwtBearerAuthentication und JwtBearerOptions.

Bei der Neuinstallation des Pakets habe ich notiert, dass die neueste Version wird fehlschlagen zu installieren - Ich versuchte Version 1.1.1 und es installiert und gab mir einen erfolgreichen Build.

Verwandte Themen