Register new user under different Tenant (Tenant impersonation)
See original GitHub issueMy 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
- I am able to set tenant using TrySetTenantInfo()
- 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:
- Created 3 years ago
- Comments:7 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@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:
Note that this line is not using the newly resolved
userManager
but instead the original_userManager
:Let me know how it goes!
Hi ok I’ll start digging in. Do you have a simple project or repo that shows the problem you can share with me?