[英]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(IApplicationBuilder, 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.