QuestionA user exists only in the metadata DB of one tenant, not in the primary one. How does the login find the right tenant, and what must I set up so it works?
AnswerThrough the
_login_index table on the primary metadata DB: you have to register the username there with
sp__login_index_upsert, otherwise the login stops at the primary DB and fails with invalid credentials.
How it worksThe login first looks for the user on the primary metadata DB. Only on a miss, and only with
multiConnectionEnabled on, the backend computes the SHA-256 of the lowercased username, reads the matching
id_azienda values from
_login_index (ascending order) and retries the login on each tenant's metadata DB. The first tenant that validates the password wins, and the cookie is issued with that tenant's
azienda_id. Because the hash is computed on the lowercase username,
Mario.Rossi and
mario.rossi hit the same index row.
Creating the user in the tenant DB does not fill the index, so after creating it run:
EXEC [dbo].[sp__login_index_upsert] @username = N'mario.rossi.t1', @idAzienda = 1;
(on MySQL, PostgreSQL and Oracle the same procedure is called with
CALL).
If the login still fails, check- the company row in
Aziende has Valida = 1 and both Connessione_DB_Dati and CONNESSIONE_DB_Meta filled: a company that does not satisfy this is not in the tenant map and its candidate is skipped. - those two columns hold the name of an entry in
ConnectionStrings, and that entry exists in the appsettings of the running backend. - the tenant DB is reachable: a connection error on one candidate is swallowed and the next one is tried, so you only see the generic invalid-credentials result.
- you changed
Aziende? The tenant map is cached: run invalidateMetadataRuntime with clearAll true.
AvoidThe same username on two tenants: the user always lands on the lowest
id_azienda whose password matches. Use distinct usernames per tenant.
Documentation: Multi-tenant