有時候咱們在ASP.NET Core項目運行時,發生在後臺程序中的錯誤會將關鍵信息隱藏爲[PII is hidden]這種佔位符,以下所示:cookie
而知道這些關鍵信息,有時候對咱們調試程序是很是重要的。因此咱們能夠在ASP.NET Core項目的Startup類中,添加IdentityModelEventSource.ShowPII = true到Configure方法中來顯示[PII is hidden]佔位符隱藏的信息:app
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.IdentityModel.Logging; namespace AspNetCorePII { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.Configure<CookiePolicyOptions>(options => { // This lambda determines whether user consent for non-essential cookies is needed for a given request. options.CheckConsentNeeded = context => false; options.MinimumSameSitePolicy = SameSiteMode.None; }); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error"); } IdentityModelEventSource.ShowPII = true; app.UseStaticFiles(); app.UseCookiePolicy(); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); } } }
可是要注意,IdentityModelEventSource.ShowPII屬性要求.NET Core項目引用了 Microsoft.IdentityModel.Logging 這個nuget包(版本大於等於5.3.0.0)才行。ui
因此這樣咱們就能夠在錯誤消息中顯示[PII is hidden]隱藏的信息了。this