在訪問ClientMvc的保護頁面時,會跳轉到IdentityMvc頁面,這時會出現相似下圖的錯誤界面,讓人無從入手。html
若是你嘗試按文字所說的內容去處理。你發現項目已正確設置。其實上面的內容是固定的,其訪問的是HomeController的Error方法,該視圖是固定了上面的內容。爲了能顯示有意義的信息咱們須要調整視圖和控制器。async
修改視圖ide
打開View/Shared/Error.cshtml,使用如下內容替換函數
@model IdentityServer4.Models.ErrorMessage @{ var error = Model?.Error; var errorDescription = Model?.ErrorDescription; var request_id = Model?.RequestId; } <div class="error-page"> <div class="page-header"> <h1>Error</h1> </div> <div class="row"> <div class="col-sm-6"> <div class="alert alert-danger"> Sorry, there was an error @if (error != null) { <strong> <em> : @error </em> </strong> if (errorDescription != null) { <div>@errorDescription</div> } } </div> @if (request_id != null) { <div class="request-id">Request Id: @request_id</div> } </div> </div> </div>
修改HomeController.cs的Error方法spa
/// <summary> /// Shows the error page /// </summary> public async Task<IActionResult> Error(string errorId) { // retrieve error details from identityserver var message = await _interaction.GetErrorContextAsync(errorId); if (message != null) { if (!_environment.IsDevelopment()) { // only show in development message.ErrorDescription = null; } } return View("Error", message); }
Error方法須要額外的兩個對象_interaction和_environment,強大的DI(依賴注入)能夠幫咱們搞定。咱們只須要調整HomeController的構造函數,以此來告訴DI咱們的控制器所須要的東西。如下是類的前面部分,包括增長的成員和構造函數。server
using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using Microsoft.AspNetCore.Hosting; using IdentityServer4.Services; using Microsoft.Extensions.Hosting; namespace IdentityMvc.Controllers { public class HomeController : Controller { private readonly IIdentityServerInteractionService _interaction; private readonly IWebHostEnvironment _environment; private readonly ILogger<HomeController> _logger; public HomeController(IWebHostEnvironment env, IIdentityServerInteractionService interaction, ILogger<HomeController> logger) { _environment = env; _interaction = interaction; _logger = logger; } //....其餘代碼
至此,跑一下程序,錯誤信息再也不模糊。htm