-
-
Notifications
You must be signed in to change notification settings - Fork 254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
.net8.0 - NullReferenceException while using the UserManager.Users #791
Comments
Hi, thanks for providing all the detail. When you hit the auth controller is there a resolved tenant at the time? |
Hi Andrew, thanks for your prompt response. There are going to be two cases when we will not have the tenant information.
For the signup, after saving the tenant information, I tried setting the TenantInfo using the
|
hi, I need to create some convenience methods for getting a user manager or similar for a given tenant without having to rely on DI. Until then consider this: HttpContext.TrySetTenantInfo(newTenantInfo, true);
// get the user manager from the new service provider scope, the one from DI was created with the original or no tenant:
var userManager = HttpContext.RequestService.GetService<UserManager<MyAppUser>>(); Once the DI scope is reset with Does that work for you? |
That's great @AndrewTriesToCode. It works after re-resolving the However for the Login functionality, as we are using a single frontend application, there is no way for us to know the Tenant of the user unless they explicitly furnish the tenant ID alongside their credentials. Is there any solution for this? As a workaround, I am thinking of creating a separate DbContext that doesn't inherit from the
and then in my Controller find the user's Tenant, set it, and then perform the operations something like below
Is this right, or is there any better way? I have to manage Tenants later down the line for support purposes and I am assuming that this separate DbContext will help me to do that (if there is no other way to allow a particular user to access the Tenant's Data). |
I created a brand new .net8.0 API project in Visual Studio 2022.
Added the .Net Core Identity support with custom User
MyAppUser
and RoleMyAppRole
classes.I added the
DbContext
class like belowInside a sample
AuthController
, I added anAuthenticate
action. Sample code snippet is given belowUpto this point calling the
Authenticate
action works fine.Now, I installed the
Finbuckle.MultiTenant.AspNetCore
andFinbuckle.MultiTenant.EntityFrameworkCore
packages.In the
Program.cs
added below lines of codeand at the end
Also added the
[MultiTenant]
attribute to theMyAppUser
andMyAppRole
classes. Finally, inherited the DbContext class fromMultiTenantIdentityDbContext
. Created and applied the migration and ran the application.Invoking the same
Authenticate
action, throws NullReferenceException atvar user = _userManager.Users.FirstOrDefault(u => u.Email == model.Email);
Stack trace is given below:
The problem persists even if the custom
User
andRole
classes are not used.See the sample github repo here
The text was updated successfully, but these errors were encountered: