From 249e64c660351df09e9fdea7b7cba2d7e891ecc3 Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Mon, 17 Aug 2020 16:00:00 +0800 Subject: [PATCH 1/6] Add Identity service error controller to account module --- .../Controllers/ErrorController.cs | 49 +++++++++++++++++++ .../Pages/Account/Error.cshtml | 24 --------- .../Pages/Account/Error.cshtml.cs | 40 --------------- 3 files changed, 49 insertions(+), 64 deletions(-) create mode 100644 modules/account/src/Volo.Abp.Account.Web.IdentityServer/Controllers/ErrorController.cs delete mode 100644 modules/account/src/Volo.Abp.Account.Web.IdentityServer/Pages/Account/Error.cshtml delete mode 100644 modules/account/src/Volo.Abp.Account.Web.IdentityServer/Pages/Account/Error.cshtml.cs diff --git a/modules/account/src/Volo.Abp.Account.Web.IdentityServer/Controllers/ErrorController.cs b/modules/account/src/Volo.Abp.Account.Web.IdentityServer/Controllers/ErrorController.cs new file mode 100644 index 00000000000..faf4539bc45 --- /dev/null +++ b/modules/account/src/Volo.Abp.Account.Web.IdentityServer/Controllers/ErrorController.cs @@ -0,0 +1,49 @@ +using System.Threading.Tasks; +using IdentityServer4.Models; +using IdentityServer4.Services; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Hosting; +using Volo.Abp.AspNetCore.Mvc; +using Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.Views.Error; +using Volo.Abp.Http; + +namespace Volo.Abp.Account.Web.Controllers +{ + [Area("account")] + public class ErrorController : AbpController + { + private readonly IIdentityServerInteractionService _interaction; + private readonly IWebHostEnvironment _environment; + + public ErrorController( + IIdentityServerInteractionService interaction, + IWebHostEnvironment environment) + { + _interaction = interaction; + _environment = environment; + } + + public async Task Index(string errorId) + { + var errorMessage = await _interaction.GetErrorContextAsync(errorId) ?? new ErrorMessage + { + Error = L["Error"] + }; + + if (!_environment.IsDevelopment()) + { + // Only show in development + errorMessage.ErrorDescription = null; + } + + + // ReSharper disable once Mvc.ViewNotResolved + return View("~/Views/Error/Default.cshtml", new AbpErrorViewModel + { + ErrorInfo = new RemoteServiceErrorInfo(errorMessage.Error, errorMessage.ErrorDescription), + HttpStatusCode = 500 + }); + } + } +} diff --git a/modules/account/src/Volo.Abp.Account.Web.IdentityServer/Pages/Account/Error.cshtml b/modules/account/src/Volo.Abp.Account.Web.IdentityServer/Pages/Account/Error.cshtml deleted file mode 100644 index 2b43f1527c0..00000000000 --- a/modules/account/src/Volo.Abp.Account.Web.IdentityServer/Pages/Account/Error.cshtml +++ /dev/null @@ -1,24 +0,0 @@ -@page -@using Localization.Resources.AbpUi -@using Microsoft.AspNetCore.Mvc.Localization -@model Volo.Abp.Account.Web.Pages.Account.ErrorModel -@inject IHtmlLocalizer L -@{ - var errorMessage = Model.ErrorMessage.Error; - var errorDetails = Model.ErrorMessage.ErrorDescription; - if (errorDetails.IsNullOrEmpty()) - { - errorDetails = errorMessage; - errorMessage = L["Error"].Value + "!"; - } -} - -

- @errorMessage -

- -
-

- @errorDetails -

-
diff --git a/modules/account/src/Volo.Abp.Account.Web.IdentityServer/Pages/Account/Error.cshtml.cs b/modules/account/src/Volo.Abp.Account.Web.IdentityServer/Pages/Account/Error.cshtml.cs deleted file mode 100644 index 90622f7dc3d..00000000000 --- a/modules/account/src/Volo.Abp.Account.Web.IdentityServer/Pages/Account/Error.cshtml.cs +++ /dev/null @@ -1,40 +0,0 @@ -using System.Threading.Tasks; -using IdentityServer4.Models; -using IdentityServer4.Services; -using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Hosting; -using Volo.Abp.AspNetCore.Mvc.UI.RazorPages; - -namespace Volo.Abp.Account.Web.Pages.Account -{ - public class ErrorModel : AbpPageModel - { - public ErrorMessage ErrorMessage { get; set; } - - private readonly IIdentityServerInteractionService _interaction; - private readonly IWebHostEnvironment _environment; - - public ErrorModel(IIdentityServerInteractionService interaction, IWebHostEnvironment environment) - { - _interaction = interaction; - _environment = environment; - } - - public async Task OnGet(string errorId) - { - ErrorMessage = await _interaction.GetErrorContextAsync(errorId) ?? new ErrorMessage - { - Error = L["Error"] - }; - - if (ErrorMessage != null) - { - if (!_environment.IsDevelopment()) - { - // Only show in development - ErrorMessage.ErrorDescription = null; - } - } - } - } -} From ce1d3523de804d8a7e0ac3ccfeab03c72e40b4df Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Mon, 17 Aug 2020 16:01:51 +0800 Subject: [PATCH 2/6] Remove resharper comment --- .../Controllers/ErrorController.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/modules/account/src/Volo.Abp.Account.Web.IdentityServer/Controllers/ErrorController.cs b/modules/account/src/Volo.Abp.Account.Web.IdentityServer/Controllers/ErrorController.cs index faf4539bc45..8a5295faa37 100644 --- a/modules/account/src/Volo.Abp.Account.Web.IdentityServer/Controllers/ErrorController.cs +++ b/modules/account/src/Volo.Abp.Account.Web.IdentityServer/Controllers/ErrorController.cs @@ -37,8 +37,6 @@ public async Task Index(string errorId) errorMessage.ErrorDescription = null; } - - // ReSharper disable once Mvc.ViewNotResolved return View("~/Views/Error/Default.cshtml", new AbpErrorViewModel { ErrorInfo = new RemoteServiceErrorInfo(errorMessage.Error, errorMessage.ErrorDescription), From 81919d7df5e0bc79b60dc5271234ef6fe1839b2c Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Mon, 17 Aug 2020 16:32:34 +0800 Subject: [PATCH 3/6] Find the error page in AbpErrorPageOptions --- .../Controllers/ErrorController.cs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/modules/account/src/Volo.Abp.Account.Web.IdentityServer/Controllers/ErrorController.cs b/modules/account/src/Volo.Abp.Account.Web.IdentityServer/Controllers/ErrorController.cs index 8a5295faa37..5c7572d74d1 100644 --- a/modules/account/src/Volo.Abp.Account.Web.IdentityServer/Controllers/ErrorController.cs +++ b/modules/account/src/Volo.Abp.Account.Web.IdentityServer/Controllers/ErrorController.cs @@ -1,10 +1,13 @@ using System.Threading.Tasks; +using AutoMapper.Internal; using IdentityServer4.Models; using IdentityServer4.Services; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Options; using Volo.Abp.AspNetCore.Mvc; +using Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared; using Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.Views.Error; using Volo.Abp.Http; @@ -15,13 +18,16 @@ public class ErrorController : AbpController { private readonly IIdentityServerInteractionService _interaction; private readonly IWebHostEnvironment _environment; + private readonly AbpErrorPageOptions _abpErrorPageOptions; public ErrorController( IIdentityServerInteractionService interaction, - IWebHostEnvironment environment) + IWebHostEnvironment environment, + IOptions abpErrorPageOptions) { _interaction = interaction; _environment = environment; + _abpErrorPageOptions = abpErrorPageOptions.Value; } public async Task Index(string errorId) @@ -37,11 +43,18 @@ public async Task Index(string errorId) errorMessage.ErrorDescription = null; } - return View("~/Views/Error/Default.cshtml", new AbpErrorViewModel + return View(GetErrorPageUrl(500), new AbpErrorViewModel { ErrorInfo = new RemoteServiceErrorInfo(errorMessage.Error, errorMessage.ErrorDescription), HttpStatusCode = 500 }); } + + private string GetErrorPageUrl(int statusCode) + { + var page = _abpErrorPageOptions.ErrorViewUrls.GetOrDefault(statusCode.ToString()); + + return string.IsNullOrWhiteSpace(page) ? "~/Views/Error/Default.cshtml" : page; + } } } From 6ed08665d9521e2de8017f4050c9ee02ca8ac273 Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Mon, 17 Aug 2020 17:15:33 +0800 Subject: [PATCH 4/6] create account area folder --- .../Account}/Controllers/ErrorController.cs | 11 +++++++---- .../Volo.Abp.Account.Web.IdentityServer.csproj | 4 ++++ 2 files changed, 11 insertions(+), 4 deletions(-) rename modules/account/src/Volo.Abp.Account.Web.IdentityServer/{ => Areas/Account}/Controllers/ErrorController.cs (85%) diff --git a/modules/account/src/Volo.Abp.Account.Web.IdentityServer/Controllers/ErrorController.cs b/modules/account/src/Volo.Abp.Account.Web.IdentityServer/Areas/Account/Controllers/ErrorController.cs similarity index 85% rename from modules/account/src/Volo.Abp.Account.Web.IdentityServer/Controllers/ErrorController.cs rename to modules/account/src/Volo.Abp.Account.Web.IdentityServer/Areas/Account/Controllers/ErrorController.cs index 5c7572d74d1..1262d0c42ce 100644 --- a/modules/account/src/Volo.Abp.Account.Web.IdentityServer/Controllers/ErrorController.cs +++ b/modules/account/src/Volo.Abp.Account.Web.IdentityServer/Areas/Account/Controllers/ErrorController.cs @@ -1,4 +1,5 @@ -using System.Threading.Tasks; +using System.Net; +using System.Threading.Tasks; using AutoMapper.Internal; using IdentityServer4.Models; using IdentityServer4.Services; @@ -11,7 +12,7 @@ using Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.Views.Error; using Volo.Abp.Http; -namespace Volo.Abp.Account.Web.Controllers +namespace Volo.Abp.Account.Web.Areas.Account.Controllers { [Area("account")] public class ErrorController : AbpController @@ -43,10 +44,12 @@ public async Task Index(string errorId) errorMessage.ErrorDescription = null; } - return View(GetErrorPageUrl(500), new AbpErrorViewModel + const int statusCode = (int)HttpStatusCode.InternalServerError; + + return View(GetErrorPageUrl(statusCode), new AbpErrorViewModel { ErrorInfo = new RemoteServiceErrorInfo(errorMessage.Error, errorMessage.ErrorDescription), - HttpStatusCode = 500 + HttpStatusCode = statusCode }); } diff --git a/modules/account/src/Volo.Abp.Account.Web.IdentityServer/Volo.Abp.Account.Web.IdentityServer.csproj b/modules/account/src/Volo.Abp.Account.Web.IdentityServer/Volo.Abp.Account.Web.IdentityServer.csproj index 566d40b3f9c..698f1f3fc9e 100644 --- a/modules/account/src/Volo.Abp.Account.Web.IdentityServer/Volo.Abp.Account.Web.IdentityServer.csproj +++ b/modules/account/src/Volo.Abp.Account.Web.IdentityServer/Volo.Abp.Account.Web.IdentityServer.csproj @@ -37,4 +37,8 @@ + + + + From cea173bd4cd1b0da72829d1044f35ce642a846d1 Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Mon, 17 Aug 2020 17:30:44 +0800 Subject: [PATCH 5/6] undo .csproj file changes --- .../Volo.Abp.Account.Web.IdentityServer.csproj | 4 ---- 1 file changed, 4 deletions(-) diff --git a/modules/account/src/Volo.Abp.Account.Web.IdentityServer/Volo.Abp.Account.Web.IdentityServer.csproj b/modules/account/src/Volo.Abp.Account.Web.IdentityServer/Volo.Abp.Account.Web.IdentityServer.csproj index 698f1f3fc9e..566d40b3f9c 100644 --- a/modules/account/src/Volo.Abp.Account.Web.IdentityServer/Volo.Abp.Account.Web.IdentityServer.csproj +++ b/modules/account/src/Volo.Abp.Account.Web.IdentityServer/Volo.Abp.Account.Web.IdentityServer.csproj @@ -37,8 +37,4 @@ - - - - From 8d17e7a86054a7eae221f68b32f68a19841f4ad0 Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Mon, 17 Aug 2020 17:43:16 +0800 Subject: [PATCH 6/6] Set ErrorController's method to virtual --- .../Areas/Account/Controllers/ErrorController.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/account/src/Volo.Abp.Account.Web.IdentityServer/Areas/Account/Controllers/ErrorController.cs b/modules/account/src/Volo.Abp.Account.Web.IdentityServer/Areas/Account/Controllers/ErrorController.cs index 1262d0c42ce..d06efee3375 100644 --- a/modules/account/src/Volo.Abp.Account.Web.IdentityServer/Areas/Account/Controllers/ErrorController.cs +++ b/modules/account/src/Volo.Abp.Account.Web.IdentityServer/Areas/Account/Controllers/ErrorController.cs @@ -31,7 +31,7 @@ public ErrorController( _abpErrorPageOptions = abpErrorPageOptions.Value; } - public async Task Index(string errorId) + public virtual async Task Index(string errorId) { var errorMessage = await _interaction.GetErrorContextAsync(errorId) ?? new ErrorMessage { @@ -53,7 +53,7 @@ public async Task Index(string errorId) }); } - private string GetErrorPageUrl(int statusCode) + protected virtual string GetErrorPageUrl(int statusCode) { var page = _abpErrorPageOptions.ErrorViewUrls.GetOrDefault(statusCode.ToString());