Cố gắng sử dụng xác thực dựa trên mã thông báo mang trong dự án API lõi Web API đơn giản. Đây là tôi Startup.cs
Xác thực mã thông báo xác thực trong ASP.NET Core
app.UseMvc();
//---
const string secretKey = "mysupersecret_secretkey!123";
SymmetricSecurityKey signingKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secretKey));
SigningCredentials signingCredentials = new SigningCredentials(signingKey, SecurityAlgorithms.HmacSha256);
//---
const string audience = "Audience";
const string issuer = "Issuer";
//---
TokenValidationParameters tokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = signingKey,
ValidateIssuer = false,
ValidIssuer = issuer,
ValidateAudience = true,
ValidAudience = audience,
ValidateLifetime = true,
ClockSkew = TimeSpan.Zero,
AuthenticationType = JwtBearerDefaults.AuthenticationScheme
};
//---
app.UseJwtBearerAuthentication(new JwtBearerOptions
{
AutomaticAuthenticate = true,
AutomaticChallenge = true,
TokenValidationParameters = tokenValidationParameters,
AuthenticationScheme = JwtBearerDefaults.AuthenticationScheme,
});
Ngoài ra tôi thêm AuthorizeAttribute
để điều khiển hành động
[HttpGet]
[Authorize(ActiveAuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
public IEnumerable<string> Get()
{
return new[] { "value1", "value2" };
}
Nhưng khi cố gắng để gửi get yêu cầu với tiêu đề Authorization: Bearer [TOKEN]
tôi nhận được ngoại lệ
System.InvalidOperationException: No authentication handler is configured to authenticate for the scheme: Bearer
at Microsoft.AspNetCore.Http.Authentication.Internal.DefaultAuthenticationManager.
Vậy là những gì 'trình xử lý xác thực' này? Trường hợp tôi cần phải thiết lập xử lý này?
bạn có thể vui lòng chia sẻ toàn bộ 'phương pháp Configure' của bạn? – Pinpoint
Shure, http://pastebin.com/TgRkHNZk app.UseSimpleTokenProvider - là một đơn giản đăng ký Mã Endpoint, tôi tìm thấy nó ở đây: https://github.com/nbarbettini/SimpleTokenProvider – Maxim