Advanced Configuration

Customizing Metrics

You may customize Coravel Pro's metrics to your liking, if you need something other than the defaults. Perhaps you want to customize the order of your metrics or simply exclude some of your metrics for now.

Clear Registered Metrics

In Configure() you should start by calling app.ClearMetrics() after app.UseCoravelPro().

This will clear any automatically registered metrics.

Add Metrics

Next, call app.UseMetric<SomeMetricType>() in the order that you want your metrics to appear in the user interface.

Example

app.UseCoravelPro();
app.ClearMetrics();
app
  .UseMetric<FirstMetric>()
  .UseMetric<SecondMetric>()
  .UseMetric<ThirdMetric>();

Authorizing Access

By default, Coravel Pro gives public access to it's routes. This is usually not what you want in production environments.

Implement IHasPermission

To easily add authorization to Coravel Pro, create a class that implements the interface Coravel.Pro.Features.Auth.Interfaces.IHasPermission.

This interface exposes one method:

bool HasPermission(HttpRequest request, ClaimsPrincipal claimsPrincipal);

You are supplied:

  • The full HTTP request
  • The ClaimsPrinciapl - usually used to verify the current User.

You may make whatever checks you need - like verifying that the User has the "Admin" role.

Return true if the user/request has permission to use Coravel Pro.

Required Services

Since Coravel Pro is designed as a native .NET Core library, you may simply inject any required services through your class' constructor.

Register Your Class

In ConfigureServices() add your class as a scoped/transitive service for the IHasPermission service type:

services.AddScoped<IHasPermission, MyPermissionClass>();

Example

This class would give permission to authenticated administrators:

public class AdminHasPermission : IHasPermission
{
    public bool HasPermission(HttpRequest request, ClaimsPrincipal claimsPrincipal)
    {
        return claimsPrincipal.Identity.IsAuthenticated && claimsPrincipal.IsInRole("Admin");
    }
}

Registering it would look like:

services.AddScoped<IHasPermission, AdminHasPermission>();