Browse Source

Added multitenancy to auth and web modules for microservice demo.

pull/221/head
Halil İbrahim Kalkan 9 years ago
parent
commit
2874436afb
  1. 2
      src/MicroserviceDemo/MicroserviceDemo.AuthServer/MicroserviceDemo.AuthServer.csproj
  2. 6
      src/MicroserviceDemo/MicroserviceDemo.AuthServer/MicroservicesAuthServerModule.cs
  3. 3
      src/MicroserviceDemo/MicroserviceDemo.AuthServer/appsettings.json
  4. 56
      src/MicroserviceDemo/MicroserviceDemo.Web/Controllers/AccountController.cs
  5. 66
      src/MicroserviceDemo/MicroserviceDemo.Web/Controllers/MultiTenancyController.cs
  6. 2
      src/MicroserviceDemo/MicroserviceDemo.Web/MicroserviceDemo.Web.csproj
  7. 7
      src/MicroserviceDemo/MicroserviceDemo.Web/MicroservicesDemoWebModule.cs
  8. 3
      src/MicroserviceDemo/MicroserviceDemo.Web/appsettings.json

2
src/MicroserviceDemo/MicroserviceDemo.AuthServer/MicroserviceDemo.AuthServer.csproj

@ -24,9 +24,11 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\Volo.Abp.Account.Web.IdentityServer\Volo.Abp.Account.Web.IdentityServer.csproj" /> <ProjectReference Include="..\..\Volo.Abp.Account.Web.IdentityServer\Volo.Abp.Account.Web.IdentityServer.csproj" />
<ProjectReference Include="..\..\Volo.Abp.AspNetCore.MultiTenancy\Volo.Abp.AspNetCore.MultiTenancy.csproj" />
<ProjectReference Include="..\..\Volo.Abp.Autofac\Volo.Abp.Autofac.csproj" /> <ProjectReference Include="..\..\Volo.Abp.Autofac\Volo.Abp.Autofac.csproj" />
<ProjectReference Include="..\..\Volo.Abp.Identity.EntityFrameworkCore\Volo.Abp.Identity.EntityFrameworkCore.csproj" /> <ProjectReference Include="..\..\Volo.Abp.Identity.EntityFrameworkCore\Volo.Abp.Identity.EntityFrameworkCore.csproj" />
<ProjectReference Include="..\..\Volo.Abp.IdentityServer.EntityFrameworkCore\Volo.Abp.IdentityServer.EntityFrameworkCore.csproj" /> <ProjectReference Include="..\..\Volo.Abp.IdentityServer.EntityFrameworkCore\Volo.Abp.IdentityServer.EntityFrameworkCore.csproj" />
<ProjectReference Include="..\..\Volo.Abp.MultiTenancy.EntityFrameworkCore\Volo.Abp.MultiTenancy.EntityFrameworkCore.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

6
src/MicroserviceDemo/MicroserviceDemo.AuthServer/MicroservicesAuthServerModule.cs

@ -7,6 +7,7 @@ using Microsoft.Extensions.DependencyInjection;
using Volo.Abp; using Volo.Abp;
using Volo.Abp.Account.Web; using Volo.Abp.Account.Web;
using Volo.Abp.AspNetCore.Modularity; using Volo.Abp.AspNetCore.Modularity;
using Volo.Abp.AspNetCore.MultiTenancy;
using Volo.Abp.AspNetCore.Mvc; using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.AspNetCore.Mvc.UI.Bootstrap; using Volo.Abp.AspNetCore.Mvc.UI.Bootstrap;
using Volo.Abp.Autofac; using Volo.Abp.Autofac;
@ -15,6 +16,7 @@ using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.Identity.EntityFrameworkCore; using Volo.Abp.Identity.EntityFrameworkCore;
using Volo.Abp.IdentityServer.EntityFrameworkCore; using Volo.Abp.IdentityServer.EntityFrameworkCore;
using Volo.Abp.Modularity; using Volo.Abp.Modularity;
using Volo.Abp.MultiTenancy.EntityFrameworkCore;
using Volo.Abp.VirtualFileSystem; using Volo.Abp.VirtualFileSystem;
namespace MicroserviceDemo.AuthServer namespace MicroserviceDemo.AuthServer
@ -23,6 +25,8 @@ namespace MicroserviceDemo.AuthServer
[DependsOn(typeof(AbpIdentityEntityFrameworkCoreModule))] [DependsOn(typeof(AbpIdentityEntityFrameworkCoreModule))]
[DependsOn(typeof(AbpIdentityServerEntityFrameworkCoreModule))] [DependsOn(typeof(AbpIdentityServerEntityFrameworkCoreModule))]
[DependsOn(typeof(AbpAccountWebIdentityServerModule))] [DependsOn(typeof(AbpAccountWebIdentityServerModule))]
[DependsOn(typeof(AbpAspNetCoreMultiTenancyModule))]
[DependsOn(typeof(AbpMultiTenancyEntityFrameworkCoreModule))]
public class MicroservicesAuthServerModule : AbpModule public class MicroservicesAuthServerModule : AbpModule
{ {
public override void ConfigureServices(IServiceCollection services) public override void ConfigureServices(IServiceCollection services)
@ -93,6 +97,8 @@ namespace MicroserviceDemo.AuthServer
app.UseDeveloperExceptionPage(); app.UseDeveloperExceptionPage();
} }
app.UseMultiTenancy();
app.UseStaticFiles(); app.UseStaticFiles();
app.UseVirtualFiles(); app.UseVirtualFiles();

3
src/MicroserviceDemo/MicroserviceDemo.AuthServer/appsettings.json

@ -1,7 +1,8 @@
{ {
"ConnectionStrings": { "ConnectionStrings": {
"Default": "Server=localhost;Database=MicroservicesDemo.Web;Trusted_Connection=True;MultipleActiveResultSets=true", "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": { "Authentication": {
"Facebook": { "Facebook": {

56
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 System.Threading.Tasks;
using IdentityModel.Client; using IdentityModel.Client;
using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using Volo.Abp.AspNetCore.MultiTenancy;
using Volo.Abp.AspNetCore.Mvc; using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.MultiTenancy;
using Volo.Abp.Ui;
namespace MicroserviceDemo.Web.Controllers namespace MicroserviceDemo.Web.Controllers
{ {
public class AccountController : AbpController public class AccountController : AbpController
{ {
private readonly ITenantStore _tenantStore;
private readonly AspNetCoreMultiTenancyOptions _options;
public AccountController(ITenantStore tenantStore, IOptions<AspNetCoreMultiTenancyOptions> options)
{
_tenantStore = tenantStore;
_options = options.Value;
}
public async Task Logout() public async Task Logout()
{ {
await HttpContext.SignOutAsync(IdentityConstants.ApplicationScheme); await HttpContext.SignOutAsync(IdentityConstants.ApplicationScheme);
@ -41,5 +56,44 @@ namespace MicroserviceDemo.Web.Controllers
ViewBag.Json = JObject.Parse(content).ToString(); ViewBag.Json = JObject.Parse(content).ToString();
return View("json"); return View("json");
} }
public async Task<ActionResult> 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<TenantInfo> FindTenantAsync(string tenantIdOrName)
{
if (Guid.TryParse(tenantIdOrName, out var parsedTenantId))
{
return await _tenantStore.FindAsync(parsedTenantId);
}
else
{
return await _tenantStore.FindAsync(tenantIdOrName);
}
}
} }
} }

66
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<AspNetCoreMultiTenancyOptions> options)
{
_tenantStore = tenantStore;
_options = options.Value;
}
public async Task<ActionResult> 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<TenantInfo> FindTenantAsync(string tenantIdOrName)
{
if (Guid.TryParse(tenantIdOrName, out var parsedTenantId))
{
return await _tenantStore.FindAsync(parsedTenantId);
}
else
{
return await _tenantStore.FindAsync(tenantIdOrName);
}
}
}
}

2
src/MicroserviceDemo/MicroserviceDemo.Web/MicroserviceDemo.Web.csproj

@ -27,11 +27,13 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\Volo.Abp.AspNetCore.Authentication.OAuth\Volo.Abp.AspNetCore.Authentication.OAuth.csproj" /> <ProjectReference Include="..\..\Volo.Abp.AspNetCore.Authentication.OAuth\Volo.Abp.AspNetCore.Authentication.OAuth.csproj" />
<ProjectReference Include="..\..\Volo.Abp.AspNetCore.MultiTenancy\Volo.Abp.AspNetCore.MultiTenancy.csproj" />
<ProjectReference Include="..\..\Volo.Abp.Autofac\Volo.Abp.Autofac.csproj" /> <ProjectReference Include="..\..\Volo.Abp.Autofac\Volo.Abp.Autofac.csproj" />
<ProjectReference Include="..\..\Volo.Abp.Identity.Application\Volo.Abp.Identity.Application.csproj" /> <ProjectReference Include="..\..\Volo.Abp.Identity.Application\Volo.Abp.Identity.Application.csproj" />
<ProjectReference Include="..\..\Volo.Abp.Identity.EntityFrameworkCore\Volo.Abp.Identity.EntityFrameworkCore.csproj" /> <ProjectReference Include="..\..\Volo.Abp.Identity.EntityFrameworkCore\Volo.Abp.Identity.EntityFrameworkCore.csproj" />
<ProjectReference Include="..\..\Volo.Abp.Identity.HttpApi\Volo.Abp.Identity.HttpApi.csproj" /> <ProjectReference Include="..\..\Volo.Abp.Identity.HttpApi\Volo.Abp.Identity.HttpApi.csproj" />
<ProjectReference Include="..\..\Volo.Abp.Identity.Web\Volo.Abp.Identity.Web.csproj" /> <ProjectReference Include="..\..\Volo.Abp.Identity.Web\Volo.Abp.Identity.Web.csproj" />
<ProjectReference Include="..\..\Volo.Abp.MultiTenancy.EntityFrameworkCore\Volo.Abp.MultiTenancy.EntityFrameworkCore.csproj" />
<ProjectReference Include="..\..\Volo.Abp.MultiTenancy.HttpApi.Client\Volo.Abp.MultiTenancy.HttpApi.Client.csproj" /> <ProjectReference Include="..\..\Volo.Abp.MultiTenancy.HttpApi.Client\Volo.Abp.MultiTenancy.HttpApi.Client.csproj" />
<ProjectReference Include="..\..\Volo.Abp.MultiTenancy.Web\Volo.Abp.MultiTenancy.Web.csproj" /> <ProjectReference Include="..\..\Volo.Abp.MultiTenancy.Web\Volo.Abp.MultiTenancy.Web.csproj" />
<ProjectReference Include="..\..\Volo.Abp.Permissions.EntityFrameworkCore\Volo.Abp.Permissions.EntityFrameworkCore.csproj" /> <ProjectReference Include="..\..\Volo.Abp.Permissions.EntityFrameworkCore\Volo.Abp.Permissions.EntityFrameworkCore.csproj" />

7
src/MicroserviceDemo/MicroserviceDemo.Web/MicroservicesDemoWebModule.cs

@ -11,6 +11,7 @@ using Swashbuckle.AspNetCore.Swagger;
using Volo.Abp; using Volo.Abp;
using Volo.Abp.AspNetCore.Authentication.OAuth; using Volo.Abp.AspNetCore.Authentication.OAuth;
using Volo.Abp.AspNetCore.Modularity; using Volo.Abp.AspNetCore.Modularity;
using Volo.Abp.AspNetCore.MultiTenancy;
using Volo.Abp.AspNetCore.Mvc.Bundling; using Volo.Abp.AspNetCore.Mvc.Bundling;
using Volo.Abp.Autofac; using Volo.Abp.Autofac;
using Volo.Abp.Data; using Volo.Abp.Data;
@ -21,6 +22,7 @@ using Volo.Abp.Identity.EntityFrameworkCore;
using Volo.Abp.Identity.Web; using Volo.Abp.Identity.Web;
using Volo.Abp.Modularity; using Volo.Abp.Modularity;
using Volo.Abp.MultiTenancy; using Volo.Abp.MultiTenancy;
using Volo.Abp.MultiTenancy.EntityFrameworkCore;
using Volo.Abp.MultiTenancy.Web; using Volo.Abp.MultiTenancy.Web;
using Volo.Abp.Permissions.EntityFrameworkCore; using Volo.Abp.Permissions.EntityFrameworkCore;
@ -34,6 +36,8 @@ namespace MicroserviceDemo.Web
[DependsOn(typeof(AbpMultiTenancyHttpApiClientModule))] [DependsOn(typeof(AbpMultiTenancyHttpApiClientModule))]
[DependsOn(typeof(AbpMultiTenancyWebModule))] [DependsOn(typeof(AbpMultiTenancyWebModule))]
[DependsOn(typeof(AbpAspNetCoreAuthenticationOAuthModule))] [DependsOn(typeof(AbpAspNetCoreAuthenticationOAuthModule))]
[DependsOn(typeof(AbpAspNetCoreMultiTenancyModule))]
[DependsOn(typeof(AbpMultiTenancyEntityFrameworkCoreModule))]
public class MicroservicesDemoWebModule : AbpModule public class MicroservicesDemoWebModule : AbpModule
{ {
public override void ConfigureServices(IServiceCollection services) public override void ConfigureServices(IServiceCollection services)
@ -70,7 +74,6 @@ namespace MicroserviceDemo.Web
services.AddAuthentication(options => services.AddAuthentication(options =>
{ {
//options.DefaultScheme = IdentityConstants.ApplicationScheme;
options.DefaultChallengeScheme = "oidc"; options.DefaultChallengeScheme = "oidc";
}) })
.AddOpenIdConnect("oidc", options => .AddOpenIdConnect("oidc", options =>
@ -124,6 +127,8 @@ namespace MicroserviceDemo.Web
app.UseDeveloperExceptionPage(); app.UseDeveloperExceptionPage();
} }
app.UseMultiTenancy();
app.UseStaticFiles(); app.UseStaticFiles();
app.UseVirtualFiles(); app.UseVirtualFiles();

3
src/MicroserviceDemo/MicroserviceDemo.Web/appsettings.json

@ -2,7 +2,8 @@
"ConnectionStrings": { "ConnectionStrings": {
"Default": "Server=localhost;Database=MicroservicesDemo.Web;Trusted_Connection=True;MultipleActiveResultSets=true", "Default": "Server=localhost;Database=MicroservicesDemo.Web;Trusted_Connection=True;MultipleActiveResultSets=true",
"AbpPermissions": "Server=localhost;Database=MicroservicesDemo.Permissions;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": { "RemoteServices": {
"AbpMultiTenancy": { "AbpMultiTenancy": {

Loading…
Cancel
Save