ASP.NET MVC. Data Driven Subdomains?

We are creating a multi-tenant web application where we identify tenants through a subdomain (customer1.ourapp.com, customer2.ourapp.com, etc.).

Subdomain settings must be data driven - i.e. we don’t want to change the IIS configuration (manually or programmatically) every time we get a new client.

In MVC, where it is best to check the correctness of the subdomain in the query (i.e. the subdomain exists in some table in the database)

Some options that I have considered

I think that conceptually this is a routing task, so does the last option seem right? those. a request with a subdomain that does not exist is essentially an invalid URL, so it should not be mapped to a route and should go through 404 instead. This will also allow us to explicitly identify routes that require or do not require a valid subdomain

+6
source share
2 answers

I would create a custom action filter and register it globally in Global.asax (don't worry when adding new controllers).

You might also consider creating a custom MvcHandler and specifying it when declaring routes. This will allow you to specify several routes (i.e., for static content) that may be available to all clients.

Another solution is to use only routing and bind to a separate domain, so you do not need to upload an expensive SSL certificate for a wildcard domain.

+1
source

I used to do this in my base controller class, but as @Jakub said, using a subdomain will be expensive if you or your client needs an SSL certificate after that.

  var dotIndex = HostingEnvironment.SiteName.IndexOf('.'); if (dotIndex > 0) { var subdomain = HostingEnvironment.SiteName.Substring(0, dotIndex); customerCode = subdomain; } 
+1
source

Source: https://habr.com/ru/post/901101/


All Articles