【问题标题】:Authentication in dot net core preview-2.0dot net core preview-2.0 中的身份验证
【发布时间】:2017-11-28 22:28:24
【问题描述】:

我在 .net-core preview-2 的 web api 项目中尝试了 jwt 令牌身份验证,但它无法正常工作。

JwtBearerAppBuilderExtensions.UseJwtBearerAuthentication(IA‌​pplicationBuilder, JwtBearerOptions)' 已过时:'见 go.microsoft.com/fwlink/?linkid=845470';

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

【问题讨论】:

  • 您应该始终将收到的任何错误消息作为文本包含在内,这样搜索就可以找到它们,而人们不必放大您的图片。
  • 错误提示:'JwtBearerAppBuilderExtensions.UseJwtBearerAuthentication(IApplicationBuilder, JwtBearerOptions)' 已过时:'See go.microsoft.com/fwlink/?linkid=845470'

标签: .net-core asp.net-core-webapi .net-core-rc2


【解决方案1】:

带有选项的配置已移至 IServiceCollection。 IApplicationBuilder 应该被告知 UseAuthentication。

app.UseAuthentication();

在 IServiceCollection 的 ConfigureServices 中,您可以使用您的选项对其进行配置。

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

【讨论】:

  • 我尝试了您的解决方案,但它向我显示了此错误:无法从 'Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerOptions' 转换为 'System.Action '
  • 它需要一个函数。你可以试试 services.AddJwtBearerAuthentication(o => { o.Audience = ..., o.Authority = ... } /跨度>
【解决方案2】:

我认为你应该使用:

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

【讨论】:

  • 这个解决方案对我不起作用。 IServiceCollection 不包含 AddJwtBearerAuthentication 的定义。你能告诉我我该如何解决这个问题。我,正在使用 .net core 2.0
  • 请安装nuget包Microsoft.AspNetCore.Authentication.JwtBearer然后你可以添加AddJwtBearerAuthentication。
【解决方案3】:

发布的版本中可以这样使用:

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

来源:https://docs.microsoft.com/en-us/aspnet/core/migration/1x-to-2x/identity-2x(参见JWT 承载认证

【讨论】:

    猜你喜欢
    • 2018-06-22
    • 2018-05-13
    • 2018-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-27
    • 2018-03-23
    • 2018-03-13
    相关资源
    最近更新 更多