Ai
1 Star 0 Fork 0

hardstylewyl/GRPC_Samples

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Program.cs 1.48 KB
一键复制 编辑 原始数据 按行查看 历史
hardstylewyl 提交于 2025-01-08 00:07 +08:00 . 未实现grpc使用https证书
using Grpc.Core;
using GrpcService1.Services;
using Microsoft.IdentityModel.Tokens;
using DemoClient = GrpcDemo.Protos.Demo.DemoClient;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHttpContextAccessor();
builder.Services.AddGrpcReflection();
builder.Services.AddGrpc(o =>
{
o.Interceptors.Add<ParamInterceptor>();
});
builder.Services.AddGrpcClient<DemoClient>(o =>
{
o.Address = new Uri("http://localhost:5210");
}).ConfigureChannel(x =>
{
x.Credentials = ChannelCredentials.SecureSsl;
});
builder.Services.AddAuthentication()
.AddJwtBearer(o =>
{
o.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = false,
ValidateAudience = false,
ValidateLifetime = false,
ValidateIssuerSigningKey = false,
IssuerSigningKey = GlobalConst.RsaPublicSecurityKey
};
});
builder.Services.AddAuthorization();
var app = builder.Build();
app.UseAuthentication(); //经过鉴权中间件,将jwt token转换为claims
app.UseAuthorization();
app.MapGrpcService<DemoService>();
if (app.Environment.IsDevelopment())
{
app.MapGrpcReflectionService().AllowAnonymous(); //启用反射,将grpc服务暴露出来(无需通过proto生成客户端也能调用),允许匿名访问
}
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

搜索帮助