5

मैं अपने प्रोजेक्ट कोर 1.1 से कोर 2.0 निर्देशों का उपयोग करने के लिए https://blogs.msdn.microsoft.com/webdev/2017/08/14/announcing-asp-net-core-2-0/ से अद्यतन किया है (अद्यतन लक्ष्य ढांचे नेट कोर 2.0 करने के लिए इस्तेमाल metapackage Microsoft.AspNetCore.All) । मैंने सभी संभावित nuget संकुल को नवीनतम संस्करणों में भी अपडेट किया है।गुम एक्सटेंशन विधि AddJwtBearerAuthentication() नेट कोर में 2.0

नेट कोर 1.1 मैं जेडब्ल्यूटी बियरर प्रमाणीकरण इस तरह से जोड़ने था:

services.AddJwtBearerAuthentication(); // from Startup.ConfigureServices() 

लेकिन विधि AddJwtBearerAuthentication:

app.UseJwtBearerAuthentication(); // from Startup.Configure() 

प्रति कोर 2.0 नया तरीका के लिए http://www.talkingdotnet.com/whats-new-in-asp-net-core-2-0/ के रूप में कॉल करने के लिए है() अनुपस्थित है। पैकेज Microsoft.AspNetCore.Authentication.JwtBearer 2.0.0 स्थापित है।

नई खाली कोर 2.0 परियोजनाओं (JwtBearer पैकेज के साथ) में IServiceCollection के लिए एक्सटेंशन विधि AddJwtBearer प्रमाणीकरण() भी नहीं है।

पुराना तरीका app.UseJwtBearerAuthentication() बिल्कुल संकलन नहीं करता है:

Error CS0619 'JwtBearerAppBuilderExtensions.UseJwtBearerAuthentication(IApplicationBuilder, JwtBearerOptions)' is obsolete: 'See https://go.microsoft.com/fwlink/?linkid=845470' 

कृपया मदद करते हैं।

उत्तर

7

ConfigureServices में JWTBearer प्रमाणीकरण कॉन्फ़िगर करने के लिए निम्न कोड का उपयोग करें:

public void ConfigureServices(IServiceCollection services) 
    { 
     services.AddAuthentication(o => 
     { 
      o.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; 
      o.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; 
     }).AddJwtBearer(o => 
     { 
      o.Authority = "https://localhost:54302"; 
      o.Audience = "your-api-id"; 
      o.RequireHttpsMetadata = false; 
     }); 

     services.AddMvc(); 
    } 

और Configure में सिर्फ UseMvc() से पहले UseAuthentication() जोड़ें:

app.UseAuthentication(); 

app.UseStaticFiles(); 

app.UseMvc(); 

एक विस्तृत उदाहरण के लिए देखें: https://github.com/aspnet/Security/blob/dev/samples/JwtBearerSample/Startup.cs#L51

0

विधि कि जेडब्ल्यूटी प्रमाणीकरण कॉन्फ़िगर करें:

// Configure authentication with JWT (Json Web Token). 
public void ConfigureJwtAuthService(IServiceCollection services) 
{ 
    // Enable the use of an [Authorize(AuthenticationSchemes = 
    // JwtBearerDefaults.AuthenticationScheme)] 
    // attribute on methods and classes to protect. 
    services.AddAuthentication().AddJwtBearer(cfg => 
    { 
    cfg.RequireHttpsMetadata = false; 
    cfg.SaveToken = true; 
    cfg.TokenValidationParameters = new TokenValidationParameters() 
    { 
     IssuerSigningKey = JwtController.SecurityKey, 
     ValidAudience = JwtController.Audience, 
     ValidIssuer = JwtController.Issuer, 
     // When receiving a token, check that we've signed it. 
     ValidateIssuerSigningKey = true, 
     // When receiving a token, check that it is still valid. 
     ValidateLifetime = true, 
     // This defines the maximum allowable clock skew 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. 
     ClockSkew = TimeSpan.FromMinutes(0) 
    }; 
    }); 
} 

अब ConfigureServices अंदर() Startup.cs की विधि, आप जेडब्ल्यूटी कॉन्फ़िगर करने के लिए ConfigureJwtAuthService() विधि कॉल कर सकते हैं प्रमाणीकरण।

संबंधित मुद्दे