Outline

Demo: Configure Authentication

In the Conduit.Api Project

Modify the Startup.cs file

Add the following code to top of the ConfigureServices Method

    var key = Encoding.ASCII.GetBytes(Configuration.GetSection("AppSettings:Token").Value);
    services
       .AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
       .AddJwtBearer(options =>
       {
          options.TokenValidationParameters = new TokenValidationParameters
          {
             ValidateIssuerSigningKey = true,
             IssuerSigningKey = new SymmetricSecurityKey(key),
             ValidateIssuer = false,
             ValidateAudience = false
          };
    });
Add the follow code to the Configure Method, between app.UseRouting() & app.UseAuthorization()

app.UseAuthentication()

Resources:

  • https://docs.microsoft.com/en-us/aspnet/core/security/authentication/?view=aspnetcore-5.0
  • https://docs.microsoft.com/en-us/aspnet/core/fundamentals/startup?view=aspnetcore-5.0
 

I finished! On to the next chapter