|
目录
简介
创建gRPC
创建服务端
创建控制台测试
- [/code][list=1]
- [*]打开服务端.csproj文件,复制以下内容粘贴到客户端的.csproj文件中并修改GrpcServices=Client,客户端.csproj文件出现 None Update="Protos\greet.proto" 这组ItemGroup是可以删除的
- [/list][code]<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>
复制代码 4.将服务端Protos文件夹及内容全部拷贝到客户端项目下
5.在客户端创建gRpcRequest.cs文件并增加下列代码,端口号填写服务端端口6.客户端Program.cs文件中加入测试代码 await gRpcRequest.SayHello();
7.服务端和客户端在想要调试的位置打上断点
8.运行服务端
9.选中客户端项目右键 -> 调试 -> 启动新实例 即可两个项目全部命中断点进行调试测试
创建自定义服务
- 服务端
- Protos文件夹添加 custom.proto文件并添加下列代码,重新生成项目打开项目所在文件夹,打开路径:obj\Debug\net6.0\Protos 查看CustomGrpc.cs是否存在,如果没有存在则在.csproj文件中添加:
- syntax = "proto3";option csharp_namespace = "Custom.Service";package custom;service CustomGreeter {<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>rpc Plus(Number) returns (NumberResult) ;}message Number {<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>int32 leftNumber = 1;<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>int32 rightNumber = 2;}<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>message NumberResult{<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>int32 result = 1;}
复制代码
- Services文件夹下添加CustomGreeterService.cs文件,namespace 与 .protos中的csharp_namespace对应
- using Grpc.Core;namespace Custom.Service{<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>public class CustomGreeterService : CustomGreeter.CustomGreeterBase<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>{<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>public override async Task Plus(Number request, ServerCallContext context) =><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup> await Task.FromResult(new NumberResult()<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>{<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>Result = request.LeftNumber + request.RightNumber,<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>});<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>}}
复制代码 3.在Program.cs 中注册新创建的服务,加入下列代码:
app.MapGrpcService();
- 客户端
- 将服务端的custom.proto文件拷贝到Protos文件夹内并在.csproj文件中添加(注意这里GrpcServices="Client"):
- 重新生成项目并检查路径: obj\Debug\net6.0\Protos 是否生成对应的CustomGrpc.cs文件
- 加入测试代码:
- public static async Task Plus(int leftNumber,int rightNumber){<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>using (var channel = GrpcChannel.ForAddress("https://localhost:7166"))<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>{<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>CustomGreeterClient client = new CustomGreeterClient(channel);<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>NumberResult number = await client.PlusAsync(new Custom.Service.Number() { LeftNumber = leftNumber,RightNumber = rightNumber});<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>Console.WriteLine(number.Result);<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>}}
复制代码
服务器流式处理方法
custom.proto
- syntax = "proto3";option csharp_namespace = "Custom.Service";package custom;service CustomGreeter {<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>rpc SelfIncreaseServer(IntArrayModel) returns (stream BathTheCatResp); //服务端流}message BathTheCatResp{<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>string message = 1;}message IntArrayModel{<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>repeated int32 number = 1;}
复制代码 CustomGreeterService.cs
gRpcRequest.cs
客户端流式处理方法
custom.proto
- syntax = "proto3";option csharp_namespace = "Custom.Service";package custom;service CustomGreeter {<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>rpc SelfIncreaseClient(stream BathTheCatReq) returns (IntArrayModel); //客户端流}message BathTheCatResp{<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>string message = 1;}message IntArrayModel{<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>repeated int32 number = 1;}
复制代码 CustomGreeterService.cs
gRpcRequest.cs
双向流式处理方法
custom.proto
- syntax = "proto3";option csharp_namespace = "Custom.Service";package custom;service CustomGreeter {<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>rpc SelfIncreaseDouble(stream BathTheCatReq) returns (stream BathTheCatResp);//双端流}message BathTheCatReq{<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>int32 id = 1;}message BathTheCatResp{<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>string message = 1;}
复制代码 CustomGreeterService.cs
gRpcRequest.cs
.Net Core 调用gRpc
项目引用
- <ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>
复制代码 Program.cs
- //CustomGreeterClient grpc连接类builder.Services.AddGrpcClient(options =>{<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>options.Address = new Uri("https://localhost:7166"); //grpc 服务地址});
复制代码 gRpcController.cs
支持Aop
- 服务端, 客户端 都需要继承 Interceptor
- 重新需要实现Aop的方法,如服务端: UnaryServerHandler,客户端: AsyncUnaryCall 针对一元调用的Aop
- 我这里使用的是NLog写的日志,NLog可以使用可以翻阅我先前的博客
.Net Core NLog+oracel
服务端 Program.cs
- builder.Services.AddGrpc(options =>{<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>options.Interceptors.Add();});
复制代码 服务端 LogInterceptor.cs
客户端 Program.cs
- builder.Services.AddGrpcClient(options =>{<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>options.Address = new Uri("https://localhost:7166"); //服务端地址}).AddInterceptor();
复制代码 客户端 LogInterceptor.cs
jwt+gRPC验证
- 准备单独的一个网站发布jwt Token (授权中心)
- 然后在gRPC项目中jwt鉴权 和 获取到角色信息之后授权
- webapi测试gRPC调用, 先在授权中心获取token然后在 ConfigureChannel 方法中设置gRPC全局的jwt token
- 返回401: jwt鉴权不通过
- 返回403: jwt授权不通过
准备Jwt Token发布中心
Program.cs
- //读取Jwt配置
- builder.Services.Configure<JwtConfig>(builder.Configuration.GetSection("JwtTokenOptions"));
复制代码 JwtConfig.cs
- namespace AuthenorizationCenter.Tools.Model{<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>public class JwtConfig<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>{<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>public string? Audience { get; set; }<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>public string? Issuer { get; set; }<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>public string? SecurityKey { get; set; }<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>public int ExpiresMinutes { get; set; }<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>}}
复制代码 AuthenorizationController.cs
- using AuthenorizationCenter.Tools;using AuthenorizationCenter.Tools.Model;using Microsoft.AspNetCore.Http;using Microsoft.AspNetCore.Mvc;using Microsoft.Extensions.Options;namespace AuthenorizationCenter.Controllers{<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>[Route("api/[controller]")]<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>[ApiController]<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>public class AuthenorizationController : ControllerBase<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>{<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>JwtConfig _jwtconfig;<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>public AuthenorizationController(IOptions jwtconfig)<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup> {<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>_jwtconfig = jwtconfig.Value;<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>}<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>[HttpGet]<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>public async Task GetToken(string userName, string passWord)<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>{<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>string token = JwtHeleper.GetToken(new()<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>{<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>UserName = userName,<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>Extended1 = "无信息",<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>Role = new List()<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>{<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>new RoleInfo() { Id = "1",Role="系统管理员"} ,<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>new RoleInfo() { Id = "2",Role="用户管理员"} ,<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>}<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>}, new()<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>{<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>Audience = _jwtconfig.Audience,<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>Issuer = _jwtconfig.Issuer,<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>SecurityKey = _jwtconfig.SecurityKey,<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>ExpiresMinutes = 5,<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>});<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>await Task.CompletedTask;<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>return token;<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>}<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>}}
复制代码 JwtHeleper.cs
- using AuthenorizationCenter.Tools.Model;using Microsoft.IdentityModel.Tokens;using System.IdentityModel.Tokens.Jwt;using System.Security.Claims;using System.Text;namespace AuthenorizationCenter.Tools{<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>public class JwtHeleper<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>{<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>public static string GetToken(UserInfo user, JwtConfig jwtConfig)<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>{<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>List claims = new List<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>{<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>new Claim(ClaimTypes.Name, user.UserName ?? ""),<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>new Claim("Extended1", user.Extended1 ?? ""),<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>new Claim("Extended2", user.Extended2 ?? ""),<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>new Claim("Extended3", user.Extended3 ?? ""),<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>new Claim("Extended4", user.Extended4 ?? ""),<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>new Claim("Extended5", user.Extended5 ?? ""),<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>};<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>if (user.Role is not null)<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>{<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>foreach (var item in user.Role)<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>{<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>claims.Add(new Claim(item.Id.ToString(), item.Role));<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>}<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>}<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>if (jwtConfig.SecurityKey == null)<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>{<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>throw new Exception("JwtConfig.SecurityKey 不能为空");<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>}<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>SymmetricSecurityKey key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtConfig.SecurityKey));<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>SigningCredentials creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>JwtSecurityToken token = new JwtSecurityToken(<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>issuer: jwtConfig.Issuer,<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>audience: jwtConfig.Audience,<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>claims: claims,<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>expires: DateTime.UtcNow.AddMinutes(jwtConfig.ExpiresMinutes),<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>signingCredentials: creds<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>);<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>string resultToken = new JwtSecurityTokenHandler().WriteToken(token);<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>return resultToken;<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>}<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>}}
复制代码 RoleInfo.cs
- namespace AuthenorizationCenter.Tools.Model{<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>public class RoleInfo<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>{<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>public string Id { get; set; }<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>public string Role { get; set; }<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>}}
复制代码 UserInfo.cs
- namespace AuthenorizationCenter.Tools.Model{<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>public class UserInfo<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>{<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>public string? UserName { get; set; }<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>public List? Role { get; set; }<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>public string? Extended1 { get; set; }<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>public string? Extended2 { get; set; }<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>public string? Extended3 { get; set; }<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>public string? Extended4 { get; set; }<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>public string? Extended5 { get; set; }<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>}}
复制代码 appsetting.json
- {<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>"JwtTokenOptions": {<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>"Issuer": "https://localhost:7117",<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>"Audience": "https://localhost:7117",<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>"SecurityKey": "kq4DY5N1eFJhscOkI7Zp4Nd0WNy9d9AEsN6Yjgdv9OxLyol66tzGBKT_7vwolN7GZ8EDwqJBwccjDJfb81ws5s3sbbP5wUzQ3-PcTSsD-Rueiu2rsOUZwg_NR3RBCwmtouV-832YV2trCjNTawLB1z0LMukWGFNaAJVZ8WdQcrYn6a0ko5oVhZqaHBgsCLEGiqPtoFsiCcrJTz1IvXHk9_cDSr2hwEmSl18GlkOtgCHFH8aidYth3aQHRHuClTi6Y9mYRJtqqK-FNQYq4ZP23DSGZGFejJFTnM9YMpppuTMLklhSGySwX8rfjZ_0L5ac18nHaykTaiC2fvH00W42qQ"<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>}}
复制代码 gRPC准备
Program.cs
- JwtConfig jwtConfig = new JwtConfig();builder.Configuration.Bind("JwtTokenOptions", jwtConfig);builder.Services.AddAuthentication(options =>{<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;}).AddJwtBearer(options =>{<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>options.TokenValidationParameters = new TokenValidationParameters()<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>{<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>ValidateIssuer = true,<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>ValidIssuer = jwtConfig.Issuer, //发行人<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>ValidateAudience = true,<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>ValidAudience = jwtConfig.Audience,//订阅人<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>ValidateIssuerSigningKey = true,<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>//对称加密密钥<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtConfig.SecurityKey!)),<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>ValidateLifetime = true, //验证失效时间<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>ClockSkew = TimeSpan.FromSeconds(30), //过期时间容错值<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>RequireExpirationTime = true,<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>AudienceValidator = (audiences, securityToken, validationParameters) =><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>{<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>return true;<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>},<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>LifetimeValidator = (notBefore, expires, securityToken, validationParameters) =><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>{<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>return true;<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>}<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>};});builder.Services.AddTransient();builder.Services.AddTransient();builder.Services.AddAuthorization(options =>{<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>options.AddPolicy("JwtPolicy", policy =><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>{<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>//jwt 授权<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>//这里为自定义授权指定一下类<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>.AddRequirements(new UserRoleRequirement(JwtBearerDefaults.AuthenticationScheme));<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>});});
复制代码 CustomGreeterService.cs
- using Grpc.Core;using Microsoft.AspNetCore.Authentication.JwtBearer;using Microsoft.AspNetCore.Authorization;namespace Custom.Service{<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>public class CustomGreeterService : CustomGreeter.CustomGreeterBase<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>{<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme,Policy = "JwtPolicy")]<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>public override async Task Plus(Number request, ServerCallContext context) =><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup> await Task.FromResult(new NumberResult()<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>{<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>Result = request.LeftNumber + request.RightNumber,<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>});<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>}}
复制代码 JwtAuthorization.cs
客户端调用
Program.cs
- builder.Services.AddGrpcClient(options =>{<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>options.Address = new Uri("https://localhost:7166");}).AddInterceptor().ConfigureChannel(async config =>{<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>//所有调用自动添加 Authorization<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>CallCredentials credentials = CallCredentials.FromInterceptor(async (context, metadata) =><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>{<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>string token = await HttpClientHelper.HttpGetAsync("https://localhost:7117/api/Authenorization?userName=admin&passWord=666");<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>metadata.Add("Authorization", $"Bearer {token}");<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>});<ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup><ItemGroup>
- <Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
- </ItemGroup>config.Credentials = ChannelCredentials.Create(new SslCredentials(), credentials);});
复制代码 HttpClientHelper.cs
gRpcController.cs
Rpc 与 Restful 区别
- RPC是以一种调用本地方法的思路来调用远程方法,通过各种RPC框架隐藏调用远程方法的细节,让用户以为调用的就是本地方法。RPC隐藏了底层网络通信的复杂度,让我们更专注于业务逻辑的开发。
- REST通过HTTP实现,把用户的需求抽象成对资源的操作,用户必须通过HTTP协议的GET、HEAD、POST、PUT、DELETE、TRACE、OPTIONS七种基本操作去和服务器交互。
- RPC通常是服务器和服务器之间的通信,比如和中间件的通信,MQ、分布式缓存、分布式数据库等等。
- 而REST通常是面向客户端的(一般是浏览器),他们的使用场景也是不一样的。
来源:https://www.cnblogs.com/qfccc/archive/2023/08/09/17617999.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作! |
|