ASP.NET Core中如何顯示[PII is hidden]的隱藏信息


有時候我們在ASP.NET Core項目運行時,發生在后台程序中的錯誤會將關鍵信息隱藏為[PII is hidden]這種占位符,如下所示:

而知道這些關鍵信息,有時候對我們調試程序是非常重要的。所以我們可以在ASP.NET Core項目的Startup類中,添加IdentityModelEventSource.ShowPII = trueConfigure方法中來顯示[PII is hidden]占位符隱藏的信息:

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)才行。

所以這樣我們就可以在錯誤消息中顯示[PII is hidden]隱藏的信息了。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM