You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Elsa Workflow API多租户配置异常:租户解析逻辑未触发

Elsa API多租户配置问题:租户解析策略未被调用

我正在一个已有的大型WebAPI应用中配置Elsa API的多租户功能,按照官方文档配置后,发现用于确定TenantId的核心策略代码从未被触发(断点未命中)。为了测试需求,我自定义了认证逻辑(实际应用需要灵活的客户端认证方式),但问题依然存在。

以下是可复现问题的最小代码片段:

using System.Security.Claims;
using System.Text.Encodings.Web;
using Elsa.Common.Multitenancy;
using Elsa.EntityFrameworkCore.Extensions;
using Elsa.EntityFrameworkCore.Modules.Management;
using Elsa.Extensions;
using Elsa.Http;
using Elsa.Identity.Multitenancy;
using Elsa.Tenants.Extensions;
using Microsoft.AspNetCore.Authentication;
using Microsoft.Extensions.Options;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddMyAuthentication("TestUser");

builder.Services.AddElsa(elsa =>
{
    elsa.UseTenants(tenants =>
    {
        tenants.ConfigureMultitenancy(options =>
        {
            // ====> 这段代码从未被调用
            options.TenantResolverPipelineBuilder.Append<ClaimsTenantResolver>();
        });
        tenants.UseConfigurationBasedTenantsProvider(options =>
        {
            options.Tenants.Add(new Tenant { Id = "1", Name = "1", TenantId = "1" });
            options.Tenants.Add(new Tenant { Id = "2", Name = "2", TenantId = "2" });
        });
    });
    elsa.UseWorkflowManagement(management => management
        .UseEntityFrameworkCore(ef => ef
        .UseSqlite()));

    elsa.UseWorkflowsApi();
    elsa.UseJavaScript();
});
builder.Services.AddControllers();

var app = builder.Build();

app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();

app.UseWorkflowsApi();

app.Run();


public static class MyAuthenticationExtensions
{
    public static AuthenticationBuilder AddMyAuthentication(this IServiceCollection services, string name, params string[] roles)
        => services
            .AddAuthentication(MyAuthenticationHandler.SchemeName)
            .AddScheme<MyAuthenticationSchemeOptions, MyAuthenticationHandler>(
                MyAuthenticationHandler.SchemeName, options =>
                {
                    options.Name = name;
                    options.Roles = roles;
                });

    public class MyAuthenticationSchemeOptions : AuthenticationSchemeOptions
    {
        public string? Name { get; set; }
        public string[]? Roles { get; set; }
    }

    public class MyAuthenticationHandler(
        IOptionsMonitor<MyAuthenticationExtensions.MyAuthenticationSchemeOptions> options,
        ILoggerFactory logger,
        UrlEncoder encoder) : AuthenticationHandler<MyAuthenticationSchemeOptions>(options, logger, encoder)
    {

        public static string SchemeName => "MyAuthentication";
        protected override Task<AuthenticateResult> HandleAuthenticateAsync()
        {
            var identity = new ClaimsIdentity(
                new[]
                {
                    new Claim(ClaimTypes.Name, Options.Name ?? "DefaultUser"),
                }
                .Concat(
                    Options.Roles?.Select(role => new Claim(ClaimTypes.Role, role)) ?? Enumerable.Empty<Claim>()
                )
                ,
                Scheme.Name);
            identity.AddClaim(new("permissions", "read:activity-descriptors"));
            identity.AddClaim(new("permissions", "read:storage-drivers"));
            identity.AddClaim(new("permissions", "read:expression-descriptors"));

            identity.AddClaim(new("permissions", "read:workflow-definitions"));

            identity.AddClaim(new("permissions", "delete:workflow-definitions"));
            identity.AddClaim(new("permissions", "write:workflow-definitions"));
            identity.AddClaim(new("permissions", "publish:workflow-definitions"));

            // identity.AddClaim(new("permissions", "*"));
            var principal = new ClaimsPrincipal(identity);
            var ticket = new AuthenticationTicket(principal, Scheme.Name);

            return Task.FromResult(AuthenticateResult.Success(ticket));
        }
    }
}

内容的提问来源于stack exchange,提问作者Stephane

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 23:04:49