在使用 Finbuckel 包的 WithPerTenantOptions 身份验证方法时,我遇到问题“TenantInfo”不包含“JwtAuthority”的定义,并且找不到接受“TenantInfo”类型的第一个参数的可访问扩展方法“JwtAuthority”(您是吗?缺少 using 指令或程序集引用?)
以下是我的Program.cs文件代码
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();
我也尝试过使用另一种方法cookie认证方法
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();
上面的方法没有报错,但是生成token失败。
我已经尝试了上面提到的两种方法,我希望创建基于租户的身份验证令牌。
您需要扩展 TenantInfo 并添加此属性
然后您注册这个新类型 -
services.AddMultiTenant<ApplicationTenantInfo>()
然后你可以注入
IMultiTenantContextAccessor<ApplicationTenantInfo> multiTenantContextAccessor
,TenantInfo将是你的类型,并带有附加属性