From a5fd8d219591c3a26dcfb49f4349d4d9f0043b48 Mon Sep 17 00:00:00 2001 From: maliming Date: Fri, 18 Jun 2021 13:41:16 +0800 Subject: [PATCH] Change the current tenant via querystrings. --- .../AbpAccountWebModule.cs | 2 + .../Pages/Account/AccountPageModel.cs | 8 ---- .../Pages/Account/ResetPassword.cshtml.cs | 42 ++++++++++++++++++- .../Volo.Abp.Account.Web.csproj | 1 + 4 files changed, 44 insertions(+), 9 deletions(-) diff --git a/modules/account/src/Volo.Abp.Account.Web/AbpAccountWebModule.cs b/modules/account/src/Volo.Abp.Account.Web/AbpAccountWebModule.cs index 4a806b9ae2..f845b7f273 100644 --- a/modules/account/src/Volo.Abp.Account.Web/AbpAccountWebModule.cs +++ b/modules/account/src/Volo.Abp.Account.Web/AbpAccountWebModule.cs @@ -3,6 +3,7 @@ using Microsoft.Extensions.DependencyInjection; using Volo.Abp.Account.Localization; using Volo.Abp.Account.Web.Pages.Account; using Volo.Abp.Account.Web.ProfileManagement; +using Volo.Abp.AspNetCore.MultiTenancy; using Volo.Abp.AspNetCore.Mvc.Localization; using Volo.Abp.AspNetCore.Mvc.UI.Bundling; using Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared; @@ -21,6 +22,7 @@ namespace Volo.Abp.Account.Web typeof(AbpIdentityAspNetCoreModule), typeof(AbpAutoMapperModule), typeof(AbpAspNetCoreMvcUiThemeSharedModule), + typeof(AbpAspNetCoreMultiTenancyModule), typeof(AbpExceptionHandlingModule) )] public class AbpAccountWebModule : AbpModule diff --git a/modules/account/src/Volo.Abp.Account.Web/Pages/Account/AccountPageModel.cs b/modules/account/src/Volo.Abp.Account.Web/Pages/Account/AccountPageModel.cs index c4461bd512..f7a3e373c5 100644 --- a/modules/account/src/Volo.Abp.Account.Web/Pages/Account/AccountPageModel.cs +++ b/modules/account/src/Volo.Abp.Account.Web/Pages/Account/AccountPageModel.cs @@ -37,14 +37,6 @@ namespace Volo.Abp.Account.Web.Pages.Account //identityResult.CheckErrors(LocalizationManager); //TODO: Get from old Abp } - protected virtual void CheckCurrentTenant(Guid? tenantId) - { - if (CurrentTenant.Id != tenantId) - { - throw new ApplicationException($"Current tenant is different than given tenant. CurrentTenant.Id: {CurrentTenant.Id}, given tenantId: {tenantId}"); - } - } - protected virtual string GetLocalizeExceptionMessage(Exception exception) { if (exception is ILocalizeErrorMessage || exception is IHasErrorCode) diff --git a/modules/account/src/Volo.Abp.Account.Web/Pages/Account/ResetPassword.cshtml.cs b/modules/account/src/Volo.Abp.Account.Web/Pages/Account/ResetPassword.cshtml.cs index 44317eaaa5..508f2d1447 100644 --- a/modules/account/src/Volo.Abp.Account.Web/Pages/Account/ResetPassword.cshtml.cs +++ b/modules/account/src/Volo.Abp.Account.Web/Pages/Account/ResetPassword.cshtml.cs @@ -1,7 +1,11 @@ using System; using System.ComponentModel.DataAnnotations; using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Http.Extensions; using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Options; +using Volo.Abp.AspNetCore.MultiTenancy; using Volo.Abp.Auditing; using Volo.Abp.Identity; using Volo.Abp.MultiTenancy; @@ -51,13 +55,49 @@ namespace Volo.Abp.Account.Web.Pages.Account protected virtual ITenantResolveResultAccessor TenantResolveResultAccessor { get; } - public ResetPasswordModel(ITenantResolveResultAccessor tenantResolveResultAccessor) + protected virtual AbpAspNetCoreMultiTenancyOptions AspNetCoreMultiTenancyOptions { get; } + + protected virtual AbpMultiTenancyOptions MultiTenancyOptions { get; } + + public ResetPasswordModel( + ITenantResolveResultAccessor tenantResolveResultAccessor, + IOptions aspNetCoreMultiTenancyOptions, + IOptions multiTenancyOptions) { TenantResolveResultAccessor = tenantResolveResultAccessor; + AspNetCoreMultiTenancyOptions = aspNetCoreMultiTenancyOptions.Value; + MultiTenancyOptions = multiTenancyOptions.Value; } public virtual Task OnGetAsync() { + if (MultiTenancyOptions.IsEnabled && + TenantResolveResultAccessor.Result?.AppliedResolvers?.Contains(CookieTenantResolveContributor.ContributorName) == true) + { + if (CurrentTenant.Id != TenantId) + { + if (TenantId != null) + { + Response.Cookies.Append( + AspNetCoreMultiTenancyOptions.TenantKey, + TenantId.ToString(), + new CookieOptions + { + Path = "/", + HttpOnly = false, + Expires = DateTimeOffset.Now.AddYears(10) + } + ); + } + else + { + Response.Cookies.Delete(AspNetCoreMultiTenancyOptions.TenantKey); + } + + return Task.FromResult(Redirect(HttpContext.Request.GetEncodedUrl())); + } + } + return Task.FromResult(Page()); } diff --git a/modules/account/src/Volo.Abp.Account.Web/Volo.Abp.Account.Web.csproj b/modules/account/src/Volo.Abp.Account.Web/Volo.Abp.Account.Web.csproj index ac59aa33ba..da95a01a8a 100644 --- a/modules/account/src/Volo.Abp.Account.Web/Volo.Abp.Account.Web.csproj +++ b/modules/account/src/Volo.Abp.Account.Web/Volo.Abp.Account.Web.csproj @@ -35,6 +35,7 @@ +