Base solution for your next web application
Open Closed

Different database for each tenant #7249


User avatar
0
hamberg created

Hello,

How do I setup (or where do I) so each tenant has a url of tenant1.domain.com and so that each tenant gets a different sql database? Do you have an example I can go through?  I am trying to setup 1 host with multiple tenant databases.

Thank you


5 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team

    There are related documents here. https://docs.aspnetzero.com/documents/aspnet-core-mvc/latest/Overview-Core#multi-tenancy https://docs.aspnetzero.com/documents/aspnet-core-mvc/latest/Features-Mvc-Core-Tenant-Management#tenant-management

  • User Avatar
    0
    rmorris created

    I've made the following changes to try to test this out locally:

    in \project_name\src\project_name.Web.Host\appsettings.json from "App": { "ServerRootAddress": "http://localhost:60193/", "ClientRootAddress": "http://localhost:4200/", "CorsOrigins": "http://*.mycompany.com,http://localhost:4200,http://localhost:49152", to "App": { "ServerRootAddress": "http://{TENANCY_NAME}.localhost:60193/", "ClientRootAddress": "http://{TENANCY_NAME}.localhost:4200/", "CorsOrigins": "http://*.mycompany.com,http://*.localhost:4200,http://localhost:4200,http://localhost:49152",

    in \project_name\src\project_name.Web.Host\src\assets\appconfig.json from "appBaseUrl": "http://localhost:4200", to "appBaseUrl": "http://{TENANCY_NAME}.localhost:4200",

    in \project_name\src\project_name.Web.Host\package.json from "scripts": { "start": "ng serve --host 0.0.0.0 --port 4200", to "scripts": { "start": "ng serve --host 0.0.0.0 --port 4200 --disableHostCheck true",

    in C:\Windows\System32\drivers\etc\hosts added 127.0.0.1 tenanta.localhost 127.0.0.1 tenantb.localhost 127.0.0.1 tenantc.localhost

    And now there is no tenant selection when I run the project at localhost:4200/account/login or tenanta.localhost:4200/account/login

    Further it appears that it only lets me log in under the host account, the tenant accounts I have set up just come back with invalid username or password. When I do log in with the host admin it will show no tenant on the user badge, just '\ADMIN,' and I can browse the tenants in the sidebar.

    Am I missing something? Do I need to adjust the login page? I can't find the Abp.TenantId in any cookies or headers as I saw mentioned in the aspnetboilerplate documentation either.

  • User Avatar
    0
    rmorris created

    So I missed the part about updating the remoteServiceBaseUrl in the Angular project, which sort of makes sense that I would be getting logged into only the host with it configured like that. I added the TENANCY_NAME placeholder to that setting as well, and now I'm back to getting CORS errors in the console, like

    Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://tenanta.localhost:60193/AbpUserConfiguration/GetAll?d=1562171377074. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

    Similar without a tenant.

    Co-worker has suggested trying to get this running with a non-localhost domain name with the entries added to the hosts file, I'm going to give it a shot, but... it seems like

    "CorsOrigins": "http://*.mycompany.com,http://*.localhost:4200,http://localhost:4200,http://localhost:49152",

    should be correct, and was necessary to get as far as the host login when the remoteServiceBaseUrl was missing the placeholder.

  • User Avatar
    0
    rmorris created

    Okay, so it seemed I had a correct configuration. The problem was how IIS Express was hosting the site. I had keyed into updating the bindings for the subdomains in applicationhost.config. However they weren't seeming to take effect.

    Two things - IIS doesn't restart when I start a new debugging session, and VS needs admin privileges to change IIS Express settings for the site it wants to launch. Those might be related... I shut down IIS Express in my system tray and restarted VS as administrator. It then started hosting the site with all the specified subdomains, and with the changes as documented in the links from maliming, as well as the hosts file changes, I can exercise this locally.

    As stated in the docs you can just switch tenants instead for local development... I just wanted to prove this working before I started breaking our deployment figuring this out.

  • User Avatar
    0
    hamberg created

    Thanks, It all works.