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

时间:2019-08-22
本文章向大家介绍ASP.NET Core中如何显示[PII is hidden]的隐藏信息,主要包括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]隐藏的信息了。

原文地址:https://www.cnblogs.com/OpenCoder/p/11393675.html