Finbuckel per tenant authenication using JWT

13 views Asked by At

While using the WithPerTenantOptions authenication method of Finbuckel package I am getting the issue 'TenantInfo' does not contain a definition for 'JwtAuthority' and no accessible extension method 'JwtAuthority' accepting a first argument of type 'TenantInfo' could be found (are you missing a using directive or an assembly reference?)

The below is my Program.cs file code

using System.Runtime.CompilerServices;
using System.Text.Json;
using Finbuckle.MultiTenant;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Authentication.JwtBearer;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddMultiTenant\<TenantInfo\>()
.WithHostStrategy()
.WithConfigurationStore()
.WithPerTenantOptions\<JwtBearerOptions\>((options, tenantInfo) =\>
{
options.Authority = tenantInfo.JwtAuthority; // Set authority for JWT token validation
options.Audience = "YourAudience"; // Set audience for JWT token validation
options.TokenValidationParameters.ValidateIssuerSigningKey = true;
});

builder.Services.AddDbContext\<AppDbContext\>();

var app = builder.Build();

// Use the middleware to enable multi-tenancy
app.UseMultiTenant();

// Use the routing middleware
app.UseRouting();

// Define your endpoints after configuring multi-tenancy middleware and routing middleware
app.MapGet("/", (IMultiTenantContextAccessor\<TenantInfo\> multiTenantContextAccessor) =\>
{
var tenantInfo = multiTenantContextAccessor.MultiTenantContext?.TenantInfo;
if (tenantInfo != null)
{
using var scope = app.Services.CreateScope();
var dbContext = scope.ServiceProvider.GetRequiredService\<AppDbContext\>( );
var data = dbContext.GenericEntities.ToList();
return $"Hello {tenantInfo.Name}!";
}
else
{
return "Tenant information not available!";
}
});

app.MapGet("/students", (IMultiTenantContextAccessor\<TenantInfo\> multiTenantContextAccessor) =\>
{
var tenantInfo = multiTenantContextAccessor.MultiTenantContext?.TenantInfo;
if (tenantInfo != null)
{
using var scope = app.Services.CreateScope();
var dbContext = scope.ServiceProvider.GetRequiredService\<AppDbContext\>();
var schools = dbContext.GenericEntities.ToList(); // Assuming GenericEntities represents schools

        // Serialize schools data into JSON format with indentation
        var json = JsonSerializer.Serialize(schools, new JsonSerializerOptions { WriteIndented = true });
    
        // Return the formatted JSON response
        return Results.Text(json, "application/json");
    }
    else
    {
        return Results.NotFound("Tenant information not available!");
    }

});

// builder.Services.AddScoped\<ITenantInfo, MyTenantInfo\>();

app.Run();

I also tried using the another method cookie authentication method

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddMultiTenant\<TenantInfo\>()
.WithHostStrategy()
.WithConfigurationStore()
.WithPerTenantOptions\<CookieAuthenticationOptions\>((o, tenantInfo) =\>
{
o.Cookie.Name = "SignInCookie-" + tenantInfo.Name;
});

// Add authentication before WithPerTenantAuthentication()
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie();

// Ensure WithPerTenantAuthentication() is called after AddAuthentication()
builder.Services.AddMultiTenant\<TenantInfo\>()
.WithPerTenantAuthentication();

// Add DbContext
builder.Services.AddDbContext\<AppDbContext\>();

var app = builder.Build();

The above method doesn't give any error but failed to generate the token.

I have tried both the methods that are mentioned above and I am expecting to create the tenant based authenticated token.

0

There are 0 answers