.net core dot net core preview-2.0中的身份验证

.net core dot net core preview-2.0中的身份验证,.net-core,asp.net-core-webapi,.net-core-rc2,.net Core,Asp.net Core Webapi,.net Core Rc2,我在.NETCorePreview-2的WebAPI项目中尝试了jwt令牌身份验证,但它不能正常工作 JwtBearerAppBuilderExtensions.UseJWTBeareRapAuthentication(IA‌​应用程序生成器, JWTBeareOptions)“已过时:'请参阅 go.microsoft.com/fwlink/?linkid=845470' 当我尝试在dot net core 1.2中使用相同的代码时,它运行正常。我该怎么办 将带有选项的配置移动到IServic

我在.NETCorePreview-2的WebAPI项目中尝试了jwt令牌身份验证,但它不能正常工作

JwtBearerAppBuilderExtensions.UseJWTBeareRapAuthentication(IA‌​应用程序生成器, JWTBeareOptions)“已过时:'请参阅 go.microsoft.com/fwlink/?linkid=845470'

当我尝试在dot net core 1.2中使用相同的代码时,它运行正常。我该怎么办


将带有选项的配置移动到IServiceCollection。应该告诉IApplicationBuilder使用身份验证

app.UseAuthentication();
在ConfigureServices for IServiceCollection中,您可以使用选项对其进行配置

services.AddJwtBearerAuthentication( o => 
        {
            o.Audience = "someaudience";
            o.Authority = "someAuthoriy";
        });

我认为你应该使用:

 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 = Configuration.GetSection("TokenAuthentication:Issuer").Value,
            // Validate the JWT Audience (aud) claim
            ValidateAudience = true,
            ValidAudience = Configuration.GetSection("TokenAuthentication:Audience").Value,
            // Validate the token expiry
            ValidateLifetime = true,
            // If you want to allow a certain amount of clock drift, set that here:
            ClockSkew = TimeSpan.Zero
        };
        services.AddJwtBearerAuthentication(options =>
        {
            options.TokenValidationParameters = tokenValidationParameters;
        });
        services.AddAuthorization(options =>
        {
            options.DefaultPolicy = new AuthorizationPolicyBuilder(JwtBearerDefaults.AuthenticationScheme).RequireAuthenticatedUser().Build();
        });
        services.AddCors(options =>
        {
            options.AddPolicy("CorsPolicy",
                builder => builder.AllowAnyOrigin()
                .AllowAnyMethod()
                .AllowAnyHeader()
                .AllowCredentials());
        });

发布版本中可按如下方式使用:

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 = Configuration.GetSection("TokenAuthentication:Issuer").Value,
    // Validate the JWT Audience (aud) claim
    ValidateAudience = true,
    ValidAudience = Configuration.GetSection("TokenAuthentication:Audience").Value,
    // Validate the token expiry
    ValidateLifetime = true,
    // If you want to allow a certain amount of clock drift, set that here:
    ClockSkew = TimeSpan.Zero
};

services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
    .AddJwtBearer(options => {
        jwtOptions.TokenValidationParameters = tokenValidationParameters;
});

services.AddAuthorization(options =>
{
    options.DefaultPolicy = new AuthorizationPolicyBuilder(JwtBearerDefaults.AuthenticationScheme).RequireAuthenticatedUser().Build();
});
services.AddCors(options =>
{
    options.AddPolicy("CorsPolicy",
        builder => builder.AllowAnyOrigin()
        .AllowAnyMethod()
        .AllowAnyHeader()
        .AllowCredentials());
});

来源:(请参阅JWT承载身份验证)

我尝试了您的解决方案,但它显示了以下错误:无法将“Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerOptions”转换为“System.Action”。它需要一个函数。您可以尝试services.AddJwtBearerAuthentication(o=>{o.acquisition=…,o.Authority=…}不确定.Net Core 2中的选项是否都相同,使用CookieAuthentication时,他们也更改了这些选项。您应该始终将收到的任何错误消息作为文本包含在内,这样搜索就可以找到它们,而人们不必放大您的图片。错误为:“JwtBearerAppBuilderExtensions.UseJWTBeareAuthentication”(IApplicationBuilder,JWTBeareOptions)'已过时:'请参阅'此解决方案不适用于我。IServiceCollection不包含AddJWTBeareAuthentication的定义。请告诉我如何解决此问题。我正在使用.net core 2.0。请安装nuget软件包Microsoft.AspNetCore.Authentication.JwtBearer,然后您可以添加AddJWTBeareAuthentication。