grpc客户端的证书身份验证问题

时间:2020-07-17 14:04:34

标签: c# asp.net-core .net-core grpc client-certificates

我已经使用gRPC客户端(控制台应用程序)和gRPC服务器应用程序创建了演示解决方案。 (.NET Core 3上提供的模板)

我正在尝试在两者之间测试证书身份验证,但是由于某种原因,它失败并显示以下错误:

AuthenticationException: The remote certificate is invalid according to the validation procedure."

我的服务器Program.cs

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
            webBuilder.ConfigureKestrel(o =>
            {
                o.ConfigureHttpsDefaults(o =>
                {
                    o.ClientCertificateMode = ClientCertificateMode.RequireCertificate;
                    o.ServerCertificate = <MyCertificateWithPrivateKey>;
            });
        });

在客户端应用程序中,我在HttpHandler中传递了完全相同的证书,该证书传递给了我创建的通道,即

GrpcChannel.ForAddress("https://localhost:5001", new GrpcChannelOptions
            {
                HttpHandler = handler
            });

我的服务器的启动类:

 public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddGrpc();
            services.AddAuthentication(CertificateAuthenticationDefaults.AuthenticationScheme)
                .AddCertificate();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseRouting();

            app.UseAuthentication();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapGrpcService<GreeterService>();

                endpoints.MapGet("/", async context =>
                {
                    await context.Response.WriteAsync("Communication with gRPC endpoints must be made through a gRPC client. To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909");
                });
            });
        }
    }

感谢您能给我的任何帮助。

0 个答案:

没有答案