diff --git a/src/MicroserviceDemo/MicroserviceDemo.AuthServer/MicroserviceDemo.AuthServer.csproj b/src/MicroserviceDemo/MicroserviceDemo.AuthServer/MicroserviceDemo.AuthServer.csproj index 2ffe2ec9c0..39cf366b8c 100644 --- a/src/MicroserviceDemo/MicroserviceDemo.AuthServer/MicroserviceDemo.AuthServer.csproj +++ b/src/MicroserviceDemo/MicroserviceDemo.AuthServer/MicroserviceDemo.AuthServer.csproj @@ -24,9 +24,11 @@ + + diff --git a/src/MicroserviceDemo/MicroserviceDemo.AuthServer/MicroservicesAuthServerModule.cs b/src/MicroserviceDemo/MicroserviceDemo.AuthServer/MicroservicesAuthServerModule.cs index 79d42a4872..bb424816a9 100644 --- a/src/MicroserviceDemo/MicroserviceDemo.AuthServer/MicroservicesAuthServerModule.cs +++ b/src/MicroserviceDemo/MicroserviceDemo.AuthServer/MicroservicesAuthServerModule.cs @@ -7,6 +7,7 @@ using Microsoft.Extensions.DependencyInjection; using Volo.Abp; using Volo.Abp.Account.Web; using Volo.Abp.AspNetCore.Modularity; +using Volo.Abp.AspNetCore.MultiTenancy; using Volo.Abp.AspNetCore.Mvc; using Volo.Abp.AspNetCore.Mvc.UI.Bootstrap; using Volo.Abp.Autofac; @@ -15,6 +16,7 @@ using Volo.Abp.EntityFrameworkCore; using Volo.Abp.Identity.EntityFrameworkCore; using Volo.Abp.IdentityServer.EntityFrameworkCore; using Volo.Abp.Modularity; +using Volo.Abp.MultiTenancy.EntityFrameworkCore; using Volo.Abp.VirtualFileSystem; namespace MicroserviceDemo.AuthServer @@ -23,6 +25,8 @@ namespace MicroserviceDemo.AuthServer [DependsOn(typeof(AbpIdentityEntityFrameworkCoreModule))] [DependsOn(typeof(AbpIdentityServerEntityFrameworkCoreModule))] [DependsOn(typeof(AbpAccountWebIdentityServerModule))] + [DependsOn(typeof(AbpAspNetCoreMultiTenancyModule))] + [DependsOn(typeof(AbpMultiTenancyEntityFrameworkCoreModule))] public class MicroservicesAuthServerModule : AbpModule { public override void ConfigureServices(IServiceCollection services) @@ -93,6 +97,8 @@ namespace MicroserviceDemo.AuthServer app.UseDeveloperExceptionPage(); } + app.UseMultiTenancy(); + app.UseStaticFiles(); app.UseVirtualFiles(); diff --git a/src/MicroserviceDemo/MicroserviceDemo.AuthServer/appsettings.json b/src/MicroserviceDemo/MicroserviceDemo.AuthServer/appsettings.json index a53e1bcbee..4c01ff1527 100644 --- a/src/MicroserviceDemo/MicroserviceDemo.AuthServer/appsettings.json +++ b/src/MicroserviceDemo/MicroserviceDemo.AuthServer/appsettings.json @@ -1,7 +1,8 @@ { "ConnectionStrings": { "Default": "Server=localhost;Database=MicroservicesDemo.Web;Trusted_Connection=True;MultipleActiveResultSets=true", - "SqlServerCache": "Server=localhost;Database=MicroservicesDemo.Cache;Trusted_Connection=True;MultipleActiveResultSets=true" + "SqlServerCache": "Server=localhost;Database=MicroservicesDemo.Cache;Trusted_Connection=True;MultipleActiveResultSets=true", + "AbpMultiTenancy": "Server=localhost;Database=MicroservicesDemo.Tenancy;Trusted_Connection=True;MultipleActiveResultSets=true" }, "Authentication": { "Facebook": { diff --git a/src/MicroserviceDemo/MicroserviceDemo.Web/Controllers/AccountController.cs b/src/MicroserviceDemo/MicroserviceDemo.Web/Controllers/AccountController.cs index 73c2e1011f..233bd85f72 100644 --- a/src/MicroserviceDemo/MicroserviceDemo.Web/Controllers/AccountController.cs +++ b/src/MicroserviceDemo/MicroserviceDemo.Web/Controllers/AccountController.cs @@ -1,16 +1,31 @@ -using System.Net.Http; +using System; +using System.Net.Http; using System.Threading.Tasks; using IdentityModel.Client; using Microsoft.AspNetCore.Authentication; +using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Options; using Newtonsoft.Json.Linq; +using Volo.Abp.AspNetCore.MultiTenancy; using Volo.Abp.AspNetCore.Mvc; +using Volo.Abp.MultiTenancy; +using Volo.Abp.Ui; namespace MicroserviceDemo.Web.Controllers { public class AccountController : AbpController { + private readonly ITenantStore _tenantStore; + private readonly AspNetCoreMultiTenancyOptions _options; + + public AccountController(ITenantStore tenantStore, IOptions options) + { + _tenantStore = tenantStore; + _options = options.Value; + } + public async Task Logout() { await HttpContext.SignOutAsync(IdentityConstants.ApplicationScheme); @@ -41,5 +56,44 @@ namespace MicroserviceDemo.Web.Controllers ViewBag.Json = JObject.Parse(content).ToString(); return View("json"); } + + public async Task SwitchTenant(string tenant = "") + { + if (tenant.IsNullOrEmpty()) + { + HttpContext.Response.Cookies.Delete(_options.TenantKey); + } + else + { + var tenantInfo = await FindTenantAsync(tenant); + if (tenantInfo == null) + { + throw new UserFriendlyException("Unknown tenant: " + tenant); + } + + HttpContext.Response.Cookies.Append( + _options.TenantKey, + tenantInfo.Id.ToString(), + new CookieOptions + { + Expires = DateTimeOffset.Now.AddYears(1) + } + ); + } + + return Redirect("/"); + } + + private async Task FindTenantAsync(string tenantIdOrName) + { + if (Guid.TryParse(tenantIdOrName, out var parsedTenantId)) + { + return await _tenantStore.FindAsync(parsedTenantId); + } + else + { + return await _tenantStore.FindAsync(tenantIdOrName); + } + } } } diff --git a/src/MicroserviceDemo/MicroserviceDemo.Web/Controllers/MultiTenancyController.cs b/src/MicroserviceDemo/MicroserviceDemo.Web/Controllers/MultiTenancyController.cs new file mode 100644 index 0000000000..9da448980f --- /dev/null +++ b/src/MicroserviceDemo/MicroserviceDemo.Web/Controllers/MultiTenancyController.cs @@ -0,0 +1,66 @@ +using System; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Options; +using Volo.Abp.AspNetCore.MultiTenancy; +using Volo.Abp.AspNetCore.Mvc; +using Volo.Abp.MultiTenancy; +using Volo.Abp.Ui; + +namespace AbpDesk.Web.Mvc.Controllers +{ + /* TODO: This is temporary solution to switch tenant. + */ + + public class MultiTenancyController : AbpController + { + private readonly ITenantStore _tenantStore; + private readonly AspNetCoreMultiTenancyOptions _options; + + public MultiTenancyController(ITenantStore tenantStore, IOptions options) + { + _tenantStore = tenantStore; + _options = options.Value; + } + + public async Task SwitchTenant(string tenant = "") + { + if (tenant.IsNullOrEmpty()) + { + HttpContext.Response.Cookies.Delete(_options.TenantKey); + } + else + { + var tenantInfo = await FindTenantAsync(tenant); + if (tenantInfo == null) + { + throw new UserFriendlyException("Unknown tenant: " + tenant); + } + + HttpContext.Response.Cookies.Append( + _options.TenantKey, + tenantInfo.Id.ToString(), + new CookieOptions + { + Expires = DateTimeOffset.Now.AddYears(1) + } + ); + } + + return Redirect("/"); + } + + private async Task FindTenantAsync(string tenantIdOrName) + { + if (Guid.TryParse(tenantIdOrName, out var parsedTenantId)) + { + return await _tenantStore.FindAsync(parsedTenantId); + } + else + { + return await _tenantStore.FindAsync(tenantIdOrName); + } + } + } +} diff --git a/src/MicroserviceDemo/MicroserviceDemo.Web/MicroserviceDemo.Web.csproj b/src/MicroserviceDemo/MicroserviceDemo.Web/MicroserviceDemo.Web.csproj index 1e11edff91..70de5e7e7e 100644 --- a/src/MicroserviceDemo/MicroserviceDemo.Web/MicroserviceDemo.Web.csproj +++ b/src/MicroserviceDemo/MicroserviceDemo.Web/MicroserviceDemo.Web.csproj @@ -27,11 +27,13 @@ + + diff --git a/src/MicroserviceDemo/MicroserviceDemo.Web/MicroservicesDemoWebModule.cs b/src/MicroserviceDemo/MicroserviceDemo.Web/MicroservicesDemoWebModule.cs index c26d0e5644..a92426aab1 100644 --- a/src/MicroserviceDemo/MicroserviceDemo.Web/MicroservicesDemoWebModule.cs +++ b/src/MicroserviceDemo/MicroserviceDemo.Web/MicroservicesDemoWebModule.cs @@ -11,6 +11,7 @@ using Swashbuckle.AspNetCore.Swagger; using Volo.Abp; using Volo.Abp.AspNetCore.Authentication.OAuth; using Volo.Abp.AspNetCore.Modularity; +using Volo.Abp.AspNetCore.MultiTenancy; using Volo.Abp.AspNetCore.Mvc.Bundling; using Volo.Abp.Autofac; using Volo.Abp.Data; @@ -21,6 +22,7 @@ using Volo.Abp.Identity.EntityFrameworkCore; using Volo.Abp.Identity.Web; using Volo.Abp.Modularity; using Volo.Abp.MultiTenancy; +using Volo.Abp.MultiTenancy.EntityFrameworkCore; using Volo.Abp.MultiTenancy.Web; using Volo.Abp.Permissions.EntityFrameworkCore; @@ -34,6 +36,8 @@ namespace MicroserviceDemo.Web [DependsOn(typeof(AbpMultiTenancyHttpApiClientModule))] [DependsOn(typeof(AbpMultiTenancyWebModule))] [DependsOn(typeof(AbpAspNetCoreAuthenticationOAuthModule))] + [DependsOn(typeof(AbpAspNetCoreMultiTenancyModule))] + [DependsOn(typeof(AbpMultiTenancyEntityFrameworkCoreModule))] public class MicroservicesDemoWebModule : AbpModule { public override void ConfigureServices(IServiceCollection services) @@ -70,7 +74,6 @@ namespace MicroserviceDemo.Web services.AddAuthentication(options => { - //options.DefaultScheme = IdentityConstants.ApplicationScheme; options.DefaultChallengeScheme = "oidc"; }) .AddOpenIdConnect("oidc", options => @@ -124,6 +127,8 @@ namespace MicroserviceDemo.Web app.UseDeveloperExceptionPage(); } + app.UseMultiTenancy(); + app.UseStaticFiles(); app.UseVirtualFiles(); diff --git a/src/MicroserviceDemo/MicroserviceDemo.Web/appsettings.json b/src/MicroserviceDemo/MicroserviceDemo.Web/appsettings.json index 9b45159b16..176e9597e1 100644 --- a/src/MicroserviceDemo/MicroserviceDemo.Web/appsettings.json +++ b/src/MicroserviceDemo/MicroserviceDemo.Web/appsettings.json @@ -2,7 +2,8 @@ "ConnectionStrings": { "Default": "Server=localhost;Database=MicroservicesDemo.Web;Trusted_Connection=True;MultipleActiveResultSets=true", "AbpPermissions": "Server=localhost;Database=MicroservicesDemo.Permissions;Trusted_Connection=True;MultipleActiveResultSets=true", - "SqlServerCache": "Server=localhost;Database=MicroservicesDemo.Cache;Trusted_Connection=True;MultipleActiveResultSets=true" + "SqlServerCache": "Server=localhost;Database=MicroservicesDemo.Cache;Trusted_Connection=True;MultipleActiveResultSets=true", + "AbpMultiTenancy": "Server=localhost;Database=MicroservicesDemo.Tenancy;Trusted_Connection=True;MultipleActiveResultSets=true" }, "RemoteServices": { "AbpMultiTenancy": {