• Visitors can check out the Forum FAQ by clicking this link. You have to register before you can post: click the REGISTER link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. View our Forum Privacy Policy.

Multi-tenant ASP.NET core application, handling multiple tabs?

Collapse
X
  •  
  • Filter
  • Time
  • Show
Clear All
new posts

    Multi-tenant ASP.NET core application, handling multiple tabs?

    I have an admin system, the customer can select a tenant from a drop down box and website redirects to the tenant home page. The customer can continue to maintain the tenant data.

    The problem I could have is if a customer opens tenant A in tab 1, then in tab 2 opens tenant B. If they go back to tab 1 and save a customer record for example it will try to save the record against tenant b.

    I use a scoped tenant class with a connection string in it, middleware on each request checks a cookie for the tenant id and places the tenant connection string in the scoped class. The scoped class is used by the repositories to retrieve the correct tenant data.

    So in the example above tab 2 will change the cookie tenant id, the middleware will change the scoped connection string and when tab 1 saves its using tenant B connection string.

    I have a nasty JavaScript hack that keeps track of open admin tabs and closes and warns the user. It's a nasty hack, causing more issues that it's worth.

    Anyone any ideas, oh and I really want to avoid having the tenant id in the query string. I'm thinking of something earlier in the life cycle of the page to catch the change and in this case redirect the request to the home page for the tenant.

    I know it's a long shot, but any ideas? Stackoverflow has failed me. Thanks in advance.

    #2
    There is a reason why MS will throw you got of the first tenant if you enter a second one.

    I don't think there is a solution beyond adding enough state information / logic within the Web page to know what tenant the page belongs to and when the tenant no longer matches throw an error message.

    MS's solution when asked about this is to train users to use the multiple profiles that are now available on chrome and Edge, my advice would be the same.
    merely at clientco for the entertainment

    Comment


      #3
      Im not really a fan of the way MS handle switching between tenants. It's a mess tbh.

      If you look at Xero for example they do it almost exactly how I want.

      My feeling is I will have to dive into ASP.NET core lifecycle and check earlier in the page lifecycle and redirect if the value in the cookie changes.

      Comment


        #4
        Originally posted by woohoo View Post
        Im not really a fan of the way MS handle switching between tenants. It's a mess tbh.

        If you look at Xero for example they do it almost exactly how I want.

        My feeling is I will have to dive into ASP.NET core lifecycle and check earlier in the page lifecycle and redirect if the value in the cookie changes.
        How would you know the value of the cookie has changed?
        merely at clientco for the entertainment

        Comment


          #5
          Not sure, which is why im asking the question.

          Comment


            #6
            For what it's worth in case anyone is still interested. I ended up implementing SignalR.

            Each connection (tab/window) is added to a group based on username, when the tenant is selected from the drop down, javascript calls a method via ajax to set the cookie tenant value (could have done it via javascript). Then a server side method on the tenant hub is called from the client. This method broadcasts to the group and triggers a method on the browsers belonging to that group and redirects to the home page.

            A bit overkill if im honest, but found it interesting.

            Comment


              #7
              Originally posted by woohoo View Post
              For what it's worth in case anyone is still interested. I ended up implementing SignalR.

              Each connection (tab/window) is added to a group based on username, when the tenant is selected from the drop down, javascript calls a method via ajax to set the cookie tenant value (could have done it via javascript). Then a server side method on the tenant hub is called from the client. This method broadcasts to the group and triggers a method on the browsers belonging to that group and redirects to the home page.

              A bit overkill if im honest, but found it interesting.
              Sounds like a workable solution. Why the reluctance to use the tenant id in the url? If you're calling back end api's you could of taken the tenant id from a hidden form field and added it to to the api request url or as a header value on the api request.
              Make Mercia Great Again!

              Comment


                #8
                It's an option to push the tenant id in the URL or have a hidden field. It just seemed like it was an inelegant solution that meant I would have to touch more of the existing codebase. Then have to test more. Didn't really want to spend more than a few hours on this problem.

                I don't think I mentioned it but the admin system was originally just to look after one tenant - two tenants meant two different admin systems. So this new admin system was really bringing that together, along with a few other features. So I really was working with an existing codebase and database.

                Guids would have made the whole thing safer but there you go.

                Comment

                Working...
                X