Added multitenancy to auth and web modules for microservice demo.

pull/221/head
Halil İbrahim Kalkan 7 years ago
parent fc9cd0213b
commit 2874436afb

@ -24,9 +24,11 @@
<ItemGroup>
<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.Identity.EntityFrameworkCore\Volo.Abp.Identity.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>
</Project>

@ -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();

@ -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": {

@ -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<AspNetCoreMultiTenancyOptions> 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<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);
}
}
}
}

@ -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);
}
}
}
}

@ -27,11 +27,13 @@
<ItemGroup>
<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.Identity.Application\Volo.Abp.Identity.Application.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.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.Web\Volo.Abp.MultiTenancy.Web.csproj" />
<ProjectReference Include="..\..\Volo.Abp.Permissions.EntityFrameworkCore\Volo.Abp.Permissions.EntityFrameworkCore.csproj" />

@ -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();

@ -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": {

Loading…
Cancel
Save