< Summary

Information
Class: MRA.WebApi.Startup.AuthenticationStartup
Assembly: MRA.WebApi
File(s): D:\a\MiguelRomerART\MiguelRomerART\MRA.WebApi\Startup\AuthenticationStartup.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 32
Coverable lines: 32
Total lines: 55
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
AddJwtAuthentication(...)100%210%

File(s)

D:\a\MiguelRomerART\MiguelRomerART\MRA.WebApi\Startup\AuthenticationStartup.cs

#LineLine coverage
 1using Microsoft.AspNetCore.Authentication.Cookies;
 2using Microsoft.AspNetCore.Authentication.JwtBearer;
 3using Microsoft.IdentityModel.Tokens;
 4using MRA.Infrastructure.Settings.Options;
 5using System.Text;
 6
 7namespace MRA.WebApi.Startup;
 8
 9public static class AuthenticationStartup
 10{
 11    public static void AddJwtAuthentication(this IServiceCollection services, IConfiguration configuration)
 12    {
 013        var jwtSection = configuration.GetSection("Jwt");
 014        services.Configure<JwtSettings>(jwtSection);
 15
 016        var jwtOptions = jwtSection.Get<JwtSettings>();
 17
 018        services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
 019        .AddJwtBearer(options =>
 020        {
 021            options.TokenValidationParameters = new TokenValidationParameters
 022            {
 023                ValidateIssuer = true,
 024                ValidateAudience = true,
 025                ValidateLifetime = true,
 026                ValidateIssuerSigningKey = true,
 027                ValidIssuer = jwtOptions.Issuer,
 028                ValidAudience = jwtOptions.Audience,
 029                IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(jwtOptions.Key))
 030            };
 031            options.Events = new JwtBearerEvents
 032            {
 033                OnAuthenticationFailed = context =>
 034                {
 035                    Console.WriteLine($"Authentication failed: {context.Exception.Message}");
 036                    return Task.CompletedTask;
 037                },
 038                OnChallenge = context =>
 039                {
 040                    Console.WriteLine("Token validation failed.");
 041                    return Task.CompletedTask;
 042                }
 043            };
 044        });
 45
 46        //services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
 47        //    .AddCookie("Cookies", options =>
 48        //    {
 49        //        options.Cookie.Name = "CookieMiguelRomeral";
 50        //        options.LoginPath = "/Admin/Login";
 51        //    });
 52
 053        services.AddAuthorization();
 054    }
 55}