diff --git a/AspNetCoreSelectTenant/Pages/Index.cshtml.cs b/AspNetCoreSelectTenant/Pages/Index.cshtml.cs index 7410bec..dd5a59b 100644 --- a/AspNetCoreSelectTenant/Pages/Index.cshtml.cs +++ b/AspNetCoreSelectTenant/Pages/Index.cshtml.cs @@ -21,13 +21,13 @@ public IndexModel(TenantProvider tenantProvider, TenantProviderCache tenantProvi public string TenantId { get; set; } = string.Empty; [BindProperty] - public List RolesInTenant { get; set; } = new List(); + public List RolesInTenant { get; set; } = []; [BindProperty] public string AppTenantName { get; set; } = string.Empty; [BindProperty] - public List AvailableAppTenants { get; set; } = new List(); + public List AvailableAppTenants { get; set; } = []; public async Task OnGetAsync() { diff --git a/AspNetCoreSelectTenant/Pages/SwitchTenant.cshtml.cs b/AspNetCoreSelectTenant/Pages/SwitchTenant.cshtml.cs index 9480197..28e3222 100644 --- a/AspNetCoreSelectTenant/Pages/SwitchTenant.cshtml.cs +++ b/AspNetCoreSelectTenant/Pages/SwitchTenant.cshtml.cs @@ -50,7 +50,9 @@ public async Task OnGetAsync() RolesInTenant.Add(role.Value); } - TenantId = HttpContext.User.FindFirstValue("http://schemas.microsoft.com/identity/claims/tenantid"); + var tid = HttpContext.User.FindFirstValue("http://schemas.microsoft.com/identity/claims/tenantid"); + + if (tid != null) TenantId = tid; } } diff --git a/AspNetCoreSelectTenant/Program.cs b/AspNetCoreSelectTenant/Program.cs index f902b10..77d6e41 100644 --- a/AspNetCoreSelectTenant/Program.cs +++ b/AspNetCoreSelectTenant/Program.cs @@ -88,10 +88,11 @@ services.AddRazorPages().AddMvcOptions(options => { var policy = new AuthorizationPolicyBuilder() - .RequireAuthenticatedUser() - // Eanble to force tenant restrictions - .AddRequirements(new[] { new TenantRequirement() }) - .Build(); + .RequireAuthenticatedUser() + // Eanble to force tenant restrictions + .AddRequirements(new[] { new TenantRequirement() }) + .Build(); + options.Filters.Add(new AuthorizeFilter(policy)); }).AddMicrosoftIdentityUI(); diff --git a/AspNetCoreSelectTenant/Tenants/TenantAdminHandler.cs b/AspNetCoreSelectTenant/Tenants/TenantAdminHandler.cs index 0c90e27..709555b 100644 --- a/AspNetCoreSelectTenant/Tenants/TenantAdminHandler.cs +++ b/AspNetCoreSelectTenant/Tenants/TenantAdminHandler.cs @@ -9,7 +9,10 @@ public class TenantAdminHandler : AuthorizationHandler public TenantAdminHandler(IConfiguration configuration) { - tenantHomeId = configuration["AdminHomeTenant"]; + var tid = configuration["AdminHomeTenant"] + ?? throw new Exception("AdminHomeTenant configuration not set"); + + tenantHomeId = tid; } protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, TenantAdminRequirement requirement)