ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,815 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hi,
i'm trying to implement a double authentication layer in a .net core 5 API, the validation should be done in two steps ,
startup.cs file
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
// Adding Authentication
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
})
// Adding Jwt Bearer
.AddJwtBearer("SimpleUsersBearer",options =>
{
options.SaveToken = true;
options.RequireHttpsMetadata = false;
options.TokenValidationParameters = new TokenValidationParameters()
{
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("MySecretKey")),
ValidateIssuerSigningKey = true,
ValidateIssuer = false,
ValidateAudience=false,
};
}).AddJwtBearer("SuperlUsersBearer", options =>
{
options.SaveToken = true;
options.RequireHttpsMetadata = false;
options.TokenValidationParameters = new TokenValidationParameters()
{
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("MySecretKey")),
ValidateIssuerSigningKey = true,
ValidateIssuer = false,
ValidateAudience = false,
};
});
services.AddAuthorization(options =>
{
options.DefaultPolicy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.AddAuthenticationSchemes("SimpleUsersBearer")
.AddAuthenticationSchemes("SuperUsersBearer")
.Build();
});
services.AddOcelot();
}
They each need their own scheme and policy. See docs