// Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
// Add HMAC authentication
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = "HMAC";
options.DefaultChallengeScheme = "HMAC";
})
.AddHMACAuthentication(options =>
{
options.SecretKey = "your_secret_key";
});
// Add JWT token authentication
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = "http://example.com",
ValidAudience = "http://example.com",
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("your_jwt_secret_key"))
};
});
// Add authorization
services.AddAuthorization(options =>
{
options.AddPolicy("AdminOnly", policy => policy.RequireRole("Admin"));
});
}
Asp.net core MVC + HMAC 实现API请求认证,用户认证与授权,Web Token认证
最新推荐文章于 2026-01-06 19:45:54 发布

最低0.47元/天 解锁文章
634

被折叠的 条评论
为什么被折叠?



