AndrewTriesToCode / MultiTenantIdentityServer4

16 stars 7 forks source link

there is a way of getting ITenantInfo inside AccountController ?? #4

Open comm-amir-david opened 1 year ago

comm-amir-david commented 1 year ago

i am getting this error when trying to get ITenantInfo from DI inside this controller with code authorization workflow InvalidOperationException: Unable to resolve service for type 'Finbuckle.MultiTenant.ITenantInfo' while attempting to activate 'IdentityServerHost.Quickstart.UI.AccountController'.

Thanks.

AndrewTriesToCode commented 1 year ago

Hi, I need a little more information. What kind of tenant strategy are you using? What is the url being processed when you get this error?

comm-amir-david commented 1 year ago

and i am using code workflow i think the error is happening when there is no tenant found.

i am using both Pathbase like the example and even if i am using static it's makes the same error. startup.cs : services.AddMultiTenant() .WithStaticStrategy("tenant-1") .WithBasePathStrategy() .WithPerTenantAuthentication(); app.UseMultiTenant(); app.Use(async (context, next) => { var mtc = context.GetMultiTenantContext(); var tenant = mtc?.TenantInfo; if (tenant != null && mtc.StrategyInfo.StrategyType == typeof(BasePathStrategy)) { context.Request.Path.StartsWithSegments("/" + tenant.Identifier, out var matched, out var newPath); context.Request.PathBase = Path.Join(context.Request.PathBase, matched); context.Request.Path = newPath; }

            await next.Invoke();
        });

and AccountController: private readonly ITenantInfo _tenantInfo; public AccountController( UserManager userManager, SignInManager signInManager, IIdentityServerInteractionService interaction, IClientStore clientStore, IAuthenticationSchemeProvider schemeProvider, IEventService events, ITenantInfo tenantInfo) { _userManager = userManager; _signInManager = signInManager; _interaction = interaction; _clientStore = clientStore; _schemeProvider = schemeProvider; _events = events; _tenantInfo = tenantInfo;

    }

Login method POST: var t = _tenantInfo; var result = await _signInManager.PasswordSignInAsync(model.Username, model.Password, model.RememberLogin, lockoutOnFailure: true);

if you need i will upload zip file of the project i just took the example and added this to the AccountController. Thanks.