.net-core - dot net core preview-2.0 中的认证

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

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

JwtBearerAppBuilderExtensions.UseJwtBearerAuthentication(IA‌​pplicationBuilder, JwtBearerOptions)' is obsolete: 'See go.microsoft.com/fwlink/?linkid=845470';



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

enter image description here

最佳答案

我认为你应该使用:

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

关于.net-core - dot net core preview-2.0 中的认证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44753666/

相关文章:

c# - 如何在存储库模式中执行异步等待?

.net - 找不到与某些项目的目标运行时之一兼容的框架 .NETCoreApp=v1 的运行时目标

asp.net - EntityFramework Core 在加入后获得多个结果

c# - 没有属性名称的Http get和post方法

sql - EF Core、FromSqlRaw 和 USE HINT

.net - .NET Core RC2 中的登录声明

.net-core - 在线程中找不到 Abort 方法

c# - 为什么.NET Core DI 容器不注入(inject) ILogger?

c# - 如何根据属性合并两个对象列表并将重复项合并到新对象中

c# - Json.Net 和 Web API 中枚举的验证