If I understood correctly, users could simultaneously open 2 different tabs, each of which would be a different tenant. And each page should display data related to each tenant.
Thus, this means that the decision associated with the cookie or session should be discarded as the tenant belongs to each tab of the browser.
And, reading your response to Kirill Guptaβs suggestion, I understand that a hidden tenantId on every page cannot be sent to every AJAX request. Of course, one solution could be to modify your application and make sure that this always applies to every AJAX request. Otherwise, it would also cancel the global filter based on the request parameters, since tenantId may not always be there.
I think the best option is to add a segment to the url containing tenantId. For example, replacing the default route with something like the following route (if you have many different routes, you need to be very careful to avoid colliding with the route):
routes.MapRoute( name: "Default", url: "{tenant}/{controller}/{action}/{id}", defaults: new { tenant = "defaultTenant", controller = "Home", action = "Index", id = UrlParameter.Optional } );
Thus, you can make sure that the tenant will always be sent for each request, and you can also have 2 different tabs with different tenants displaying each corresponding file.
There are various options for restoring the value of a route segment.
The binding will automatically fill in the value for any parameter with the name "tenant" of your action method or any parameter with the name "tenant" in the model class, which is the parameter of the action method: public ActionResult Foo (model FooModel, tenant of strings) {// as a tenant, so and model.tenant will contain the value of the segment URL return View (); }
You can also write a filter that accesses the value of the route parameter (RouteData is a property of the ActionExecutingContext and ActionExecutedContext , which are accepted as parameters of filter methods), and performs some logic. Then the filter will be installed as a global filter in your application or on your base controller:
public class FooFilterAttribute : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { var tenant = filterContext.RouteData.Values["tenant"]
The final parameter is direct access to the RouteData parameter in your base controller class. (Since RouteData is a property of the MVC Controller base class)
As long as you use the Html and Ajax helpers to create the URLs, the tenant segment of the URL will be supported in your links. However, if you have jquery code that directly sends AJAX calls with hard-coded URLs, you will need to update that code so that the new URL segment is taken into account.
Finally, if the tenantId values ββare not very user friendly, like an integer, you can have unique names for each tenant and use the names in the URL. Then you would add some logic that maps it to the integer value your application needs.