就在本周,我在一个现有的多租户系统上解决了这个问题,该系统使用 .NET 安全主体来处理用户权限和租户。我使用自定义 ServiceRunner 来选择租户并设置安全性。您对多租户的方法是不同的,但使用 ServiceRunner 似乎仍然是一种有效的方法。
你最终会得到这样的东西:
public class MyServiceRunner<T> : ServiceRunner<T>
{
public MyServiceRunner(IAppHost appHost, ActionContext actionContext)
: base(appHost, actionContext)
{}
public override void BeforeEachRequest(IRequestContext requestContext, T request)
{
// Set backend authentication before the requests are processed.
if(request instanceof ITenantRequest)
{
Uri uri = new Uri(requestContext.AbsoluteUri);
string tenant = uri.Host; // Or whatever logic you need...
((ITenantRequest).Tenant = tenant;
}
}
}
public class MyAppHost : AppHostBase
{
public MyAppHost() : base("My Web Services", typeof(MyService).Assembly) { }
public override IServiceRunner<TRequest> CreateServiceRunner<TRequest>(ActionContext actionContext)
{
return new MyServiceRunner<TRequest>(this, actionContext);
}
public override void Configure(Container container)
{
...
}
}
也许请求过滤方法在某种程度上更好,但这对我们有用。