From 239cde8cdad2f837b34a2d55185991caf958768f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Tue, 6 Mar 2018 17:20:41 +0300 Subject: [PATCH] Added temporary code for identity server multitenancy --- .../MicroservicesAuthServerModule.cs | 5 ++- .../MicroservicesDemoWebModule.cs | 18 ++++++++- .../Pages/Account/Login.cshtml.cs | 38 ++++++++++++++----- .../AspNetCoreMultiTenancyOptions.cs | 8 ++-- .../Abp/MultiTenancy/TenantResolverConsts.cs | 7 ++++ 5 files changed, 59 insertions(+), 17 deletions(-) create mode 100644 src/Volo.Abp.MultiTenancy.Abstractions/Volo/Abp/MultiTenancy/TenantResolverConsts.cs diff --git a/src/MicroserviceDemo/MicroserviceDemo.AuthServer/MicroservicesAuthServerModule.cs b/src/MicroserviceDemo/MicroserviceDemo.AuthServer/MicroservicesAuthServerModule.cs index bb424816a9..f40c7ba0df 100644 --- a/src/MicroserviceDemo/MicroserviceDemo.AuthServer/MicroservicesAuthServerModule.cs +++ b/src/MicroserviceDemo/MicroserviceDemo.AuthServer/MicroservicesAuthServerModule.cs @@ -97,11 +97,12 @@ namespace MicroserviceDemo.AuthServer app.UseDeveloperExceptionPage(); } - app.UseMultiTenancy(); - app.UseStaticFiles(); app.UseVirtualFiles(); + app.UseAuthentication(); //TODO: This makes it twice since UseIdentityServer also internally adds UseAuthentication! + app.UseMultiTenancy(); + app.UseIdentityServer(); //This internally adds .UseAuthentication() (we should be carefully about that) app.UseMvc(routes => diff --git a/src/MicroserviceDemo/MicroserviceDemo.Web/MicroservicesDemoWebModule.cs b/src/MicroserviceDemo/MicroserviceDemo.Web/MicroservicesDemoWebModule.cs index 412332d08a..72f9b841b1 100644 --- a/src/MicroserviceDemo/MicroserviceDemo.Web/MicroservicesDemoWebModule.cs +++ b/src/MicroserviceDemo/MicroserviceDemo.Web/MicroservicesDemoWebModule.cs @@ -1,4 +1,5 @@ using System; +using System.Threading.Tasks; using Microsoft.AspNetCore.Authentication.OAuth.Claims; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; @@ -69,9 +70,9 @@ namespace MicroserviceDemo.Web { "/Abp/ApplicationConfigurationScript?_v=" + DateTime.Now.Ticks, "/Abp/ServiceProxyScript?_v=" + DateTime.Now.Ticks - }); + }); }); - + services.AddAuthentication(options => { options.DefaultChallengeScheme = "oidc"; @@ -97,6 +98,19 @@ namespace MicroserviceDemo.Web options.Scope.Add("multi-tenancy-api"); options.ClaimActions.MapAbpClaimTypes(); + + options.Events.OnRedirectToIdentityProvider += context => + { + //TODO: ... + + var tenantId = context.HttpContext.RequestServices.GetRequiredService().Id?.ToString(); + if (!tenantId.IsNullOrEmpty()) + { + context.ProtocolMessage.Parameters[TenantResolverConsts.DefaultTenantKey] = tenantId; + } + + return Task.CompletedTask; + }; }); services.Configure(configuration); diff --git a/src/Volo.Abp.Account.Web.IdentityServer/Pages/Account/Login.cshtml.cs b/src/Volo.Abp.Account.Web.IdentityServer/Pages/Account/Login.cshtml.cs index a1db7969f1..8626609524 100644 --- a/src/Volo.Abp.Account.Web.IdentityServer/Pages/Account/Login.cshtml.cs +++ b/src/Volo.Abp.Account.Web.IdentityServer/Pages/Account/Login.cshtml.cs @@ -18,6 +18,7 @@ using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Volo.Abp.Account.Web.Auth; using Volo.Abp.Identity; +using Volo.Abp.MultiTenancy; using Volo.Abp.Ui; using Volo.Abp.Uow; @@ -55,12 +56,12 @@ namespace Volo.Abp.Account.Web.Pages.Account private readonly IEventService _identityServerEvents; public IdsLoginModel( - SignInManager signInManager, - IdentityUserManager userManager, - IIdentityServerInteractionService interaction, - IAuthenticationSchemeProvider schemeProvider, - IOptions accountOptions, - IClientStore clientStore, + SignInManager signInManager, + IdentityUserManager userManager, + IIdentityServerInteractionService interaction, + IAuthenticationSchemeProvider schemeProvider, + IOptions accountOptions, + IClientStore clientStore, IEventService identityServerEvents) { _signInManager = signInManager; @@ -77,12 +78,29 @@ namespace Volo.Abp.Account.Web.Pages.Account LoginInput = new LoginInputModel(); var context = await _interaction.GetAuthorizationContextAsync(ReturnUrl); - LoginInput.UserNameOrEmailAddress = context?.LoginHint; + + if (context != null) + { + LoginInput.UserNameOrEmailAddress = context.LoginHint; + + //TODO: !!! Always exchanging tenant id, not name! + var tenant = context.Parameters[TenantResolverConsts.DefaultTenantKey]; + if (tenant.IsNullOrEmpty()) + { + Response.Cookies.Delete(TenantResolverConsts.DefaultTenantKey); + CurrentTenant.Change(null); + } + else + { + Response.Cookies.Append(TenantResolverConsts.DefaultTenantKey, tenant); + CurrentTenant.Change(Guid.Parse(tenant)); + } + } if (context?.IdP != null) { LoginInput.UserNameOrEmailAddress = context.LoginHint; - ExternalProviders = new[] {new ExternalProviderModel {AuthenticationScheme = context.IdP}}; + ExternalProviders = new[] { new ExternalProviderModel { AuthenticationScheme = context.IdP } }; return; } @@ -111,7 +129,7 @@ namespace Volo.Abp.Account.Web.Pages.Account } } } - + ExternalProviders = providers.ToArray(); if (IsExternalLoginOnly) @@ -165,7 +183,7 @@ namespace Volo.Abp.Account.Web.Pages.Account return Redirect(ReturnUrl); //ReturnUrlHash? } - + [UnitOfWork] public virtual async Task OnPostExternalLogin(string provider) { diff --git a/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/AspNetCoreMultiTenancyOptions.cs b/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/AspNetCoreMultiTenancyOptions.cs index fda1971eb1..b985aae73e 100644 --- a/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/AspNetCoreMultiTenancyOptions.cs +++ b/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/AspNetCoreMultiTenancyOptions.cs @@ -1,15 +1,17 @@ -namespace Volo.Abp.AspNetCore.MultiTenancy +using Volo.Abp.MultiTenancy; + +namespace Volo.Abp.AspNetCore.MultiTenancy { public class AspNetCoreMultiTenancyOptions { /// - /// Default: "__tenant". + /// Default: . /// public string TenantKey { get; set; } public AspNetCoreMultiTenancyOptions() { - TenantKey = "__tenant"; + TenantKey = TenantResolverConsts.DefaultTenantKey; } } } \ No newline at end of file diff --git a/src/Volo.Abp.MultiTenancy.Abstractions/Volo/Abp/MultiTenancy/TenantResolverConsts.cs b/src/Volo.Abp.MultiTenancy.Abstractions/Volo/Abp/MultiTenancy/TenantResolverConsts.cs new file mode 100644 index 0000000000..e4bf2141ca --- /dev/null +++ b/src/Volo.Abp.MultiTenancy.Abstractions/Volo/Abp/MultiTenancy/TenantResolverConsts.cs @@ -0,0 +1,7 @@ +namespace Volo.Abp.MultiTenancy +{ + public class TenantResolverConsts + { + public const string DefaultTenantKey = "__tenant"; + } +}