Ai
1 Star 0 Fork 0

hardstylewyl/GRPC_Samples

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Program.cs 2.13 KB
一键复制 编辑 原始数据 按行查看 历史
hardstylewyl 提交于 2025-01-08 00:07 +08:00 . 未实现grpc使用https证书
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using _10GrpcServer.Services;
using Microsoft.AspNetCore.Authentication.Certificate;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.AspNetCore.Server.Kestrel.Https;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddGrpc(x => { x.EnableDetailedErrors = true; });
builder.WebHost.ConfigureKestrel(o =>
{
o.ListenAnyIP(5000, listenOptions => { listenOptions.Protocols = HttpProtocols.Http2; });
// o.ListenAnyIP(5001, listenOptions =>
// {
// listenOptions.Protocols = HttpProtocols.Http2;
// listenOptions.UseHttps(X509CertificateLoader.LoadPkcs12FromFile("Certs/hardstyle.pfx", "pass1234"));
// });
o.ListenAnyIP(5005, listenOptions =>
{
listenOptions.UseHttps(httpsOptions =>
{
// var localhostCert = CertificateLoader.LoadFromStoreCert(
// "hardstyle", "My", StoreLocation.CurrentUser,
// allowInvalid: true);
var localhostCert = X509CertificateLoader.LoadPkcs12FromFile("Certs/hardstyle.pfx", "pass1234");
// var exampleCert = CertificateLoader.LoadFromStoreCert(
// "example.com", "My", StoreLocation.CurrentUser,
// allowInvalid: true);
listenOptions.UseHttps(new TlsHandshakeCallbackOptions
{
OnConnection = context =>
{
if (string.Equals(context.ClientHelloInfo.ServerName, "localhost",
StringComparison.OrdinalIgnoreCase))
{
// Different TLS requirements for this host
context.AllowDelayedClientCertificateNegotation = true;
return new ValueTask<SslServerAuthenticationOptions>(
new SslServerAuthenticationOptions
{
ServerCertificate = localhostCert
});
}
return new ValueTask<SslServerAuthenticationOptions>(
new SslServerAuthenticationOptions
{
ServerCertificate = localhostCert
});
}
});
});
});
});
var app = builder.Build();
app.MapGrpcService<GreeterService>();
app.MapGet("/", () => "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");
app.Run();
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/hardstylewyl/grpc_samples.git
git@gitee.com:hardstylewyl/grpc_samples.git
hardstylewyl
grpc_samples
GRPC_Samples
master

搜索帮助