question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Register new user under different Tenant (Tenant impersonation)

See original GitHub issue

My application is built using .NET Core 3.1 with Finbuckle.MultiTenant.AspNetCore 6.0.0.

As the super administrator, I want to create an administrator for another new tenant. Note that

  1. I am able to set tenant using TrySetTenantInfo()
  2. However when I register a new user, the user is registered under my current logged in tenant. So in the AspNetUsers table, the Tenant Id of the new user is the Id of the tenant I am logged in with.

Please can someone point me in the right direction?

My code is:

           //Correctly gets the current tenant
            var currentTenant = HttpContext.GetMultiTenantContext<ApplicationTenantInfo>()?.TenantInfo;
            
            //Correctly retrieves the target tenant
            var targetTenant = _store.TryGetAsync(model.TenantId.ToString()).Result;

            //Correctly sets the new tenant to the target tenant
            bool isSet = HttpContext.TrySetTenantInfo(targetTenant, true);

            //Verify that target tenant is correctly set, works
            var newTenant = HttpContext.GetMultiTenantContext<ApplicationTenantInfo>()?.TenantInfo;       

            if (ModelState.IsValid)
            {
                var password = RandomGenerator.GenerateRandomString(8);
                var user = new ApplicationUser { UserName = model.Email, Email = model.Email, Name = model.Name, PhoneNumber = model.PhoneNumber };                
                
                //User is not created using the target tenant
                var result = await _userManager.CreateAsync(user, password);
                //bool reset = HttpContext.TrySetTenantInfo(currentTenant, false);
            }

            var tenants = _store.GetAllAsync().Result;
            ViewBag.TenantId = new SelectList(tenants, "Id", "Identifier");
            return View(model);
        }

I have also tried:

var accessor = _serviceProvider.GetRequiredService<IMultiTenantContextAccessor>();
            var multiTenantContext = new MultiTenantContext<ApplicationTenantInfo>();
            multiTenantContext.TenantInfo = newTenant;
            accessor.MultiTenantContext = multiTenantContext;

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
AndrewTriesToCodecommented, Nov 12, 2020

@coommark I think I’ve got it and if I’m right you might want to kick yourself (I say that as someone who does this to my self frequently):

In your first attempt above:

            bool isSet = HttpContext.TrySetTenantInfo(targetTenant, true);

            //Try re-resolving the user manager from DI as per @AndrewTriesToCode          
            var userManager = HttpContext.RequestServices.GetService<UserManager<ApplicationUser>>();
            
            if (ModelState.IsValid)
            {
                var password = RandomGenerator.GenerateRandomString(8);
                var user = new ApplicationUser { UserName = model.Email, Email = model.Email, Name = model.Name, PhoneNumber = model.PhoneNumber };                
                
                
                if(result.Succeeded)
                {
                    //Send email with password, tenant name, login link                    
                }

                bool reset = HttpContext.TrySetTenantInfo(currentTenant, true);
            }

Note that this line is not using the newly resolved userManager but instead the original _userManager:

//User is not created using the target tenant
var result = await _userManager.CreateAsync(user, password);

Let me know how it goes!

1reaction
AndrewTriesToCodecommented, Nov 9, 2020

Hi ok I’ll start digging in. Do you have a simple project or repo that shows the problem you can share with me?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Configure impersonation
To configure impersonation for all users in an organization · Open the Exchange Management Shell. From the Start menu, choose All Programs > ......
Read more >
How to Enable Impersonation in Microsoft 365 or ...
Please ensure you are signed in as an admin user. Once there, head to the Roles -> Admin roles section and click Add...
Read more >
User impersonation
User impersonation. This package comes with a feature that lets you impersonate users inside tenant databases. This feature works with any identification ...
Read more >
Impersonating a User as Tenant Operator
This REST API creates a disk library as a tenant operator for the company with ID 10. POST <webservice>/Library HTTP/1.1. Host: client.mydomain.
Read more >
Setting up Application Impersonation | Enterprise Connect ...
Click on the '+' above Roles, and select ApplicationImpersonation from the list. Click Add, then OK. Step 5 #. Click on the '+'...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found