简体   繁体   English

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

[英]Authentication in dot net core preview-2.0

I tried jwt token authentication in my web api project in .net-core preview-2, but it's not working properly. 我在.net-core preview-2中的web api项目中尝试了jwt令牌认证,但它无法正常工作。

JwtBearerAppBuilderExtensions.UseJwtBearerAuthentication(IA‌​pplicationBuilder, JwtBearerOptions)' is obsolete: 'See go.microsoft.com/fwlink/?linkid=845470'; JwtBearerAppBuilderExtensions.UseJwtBearerAuthentication(IA pplicationBuilder,JwtBearerOptions)'已经过时了:'请参阅go.microsoft.com/fwlink/?linkid=845470';

When i try same code to dot net core 1.2, it runs properly. 当我尝试相同的代码点网核1.2,它运行正常。 What should i do? 我该怎么办?

在此输入图像描述

i think you should use: 我认为你应该使用:

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

In the released version it can be used like the following: 发布的版本中,它可以像下面这样使用:

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

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

The configuration with options moved to the IServiceCollection. 带有选项的配置已移至IServiceCollection。 The IApplicationBuilder should just be told to UseAuthentication. 应该告诉IApplicationBuilder使用UseAuthentication。

app.UseAuthentication();

In the ConfigureServices for IServiceCollection, you can configure it with your options. 在ConfigureServices for IServiceCollection中,您可以使用您的选项对其进行配置。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Dot net core 1工具预览离线安装程序 - Dot net core 1 Tooling Preview offline installer 将SPA中的cookie身份验证升级到.NET Core 2.0 - Upgrading cookie authentication in a SPA to .NET Core 2.0 .Net核心2.0和多重身份验证方案 - .Net core 2.0 and Multiple Authentication schemes 在.NET Core 2.0 Preview 1中使用R.NET.Community - Using R.NET.Community in .NET Core 2.0 Preview 1 安装Dot Net Core 1.0.0 VS 2015工具预览版2的问题 - Problems installing Dot Net Core 1.0.0 VS 2015 Tools Preview 2 是否需要在.NET Core 2.0和EF下使用IdentityDbContext进行JWT身份验证? - Is it required to use IdentityDbContext for JWT authentication under .NET Core 2.0 and EF? 如何在.NET Core 2.0中实现JWT身份验证? - How do you implement JWT authentication in .NET Core 2.0? 使用 .NET Core Fakes 和 VS 2019 Preview 6.9.0 Preview 2.0 找不到类型或命名空间名称 - The type or namespace name could not be found with .NET Core Fakes and VS 2019 Preview 6.9.0 Preview 2.0 ASP.NET Core 2.0 Facebook JWT身份验证 - ASP.NET core 2.0 facebook JWT authentication 没有配置身份验证处理程序来对方案进行身份验证:“承载”.net core 2.0 - no authentication handler is configured to authenticate for the scheme: "bearer" .net core 2.0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM