为了账号安全,请及时绑定邮箱和手机立即绑定

C# - etcd GRPC 客户端基本身份验证

C# - etcd GRPC 客户端基本身份验证

C#
有只小跳蛙 2021-07-09 07:06:18
我正在尝试为 etcd v3+ 实现 C# GRPC 客户端。我能够通过无身份验证和通道 ssl 身份验证进行连接。但是,我也在尝试找出基本的身份验证机制。这是我的实现。using System;using System.Threading.Tasks;using System.Collections.Generic;using Grpc.Core;using Etcdserverpb;using Google.Protobuf;using System.Runtime.CompilerServices;using Grpc.Auth;using Grpc.Core.Interceptors;namespace myproj.etcd{    public class EtcdClient    {               Channel channel;        KV.KVClient kvClient;        string host;        string username;        string password;        string authToken;        Auth.AuthClient authClient;        public EtcdClient(string host, string username, string password)        {            this.username = username;            this.password = password;            this.host = host;                             Authenticate();            // Expirementing with the token, trying to achieve my goal.            channel = new Channel(host, ChannelCredentials.Create(ChannelCredentials.Insecure,                                                                GoogleGrpcCredentials.FromAccessToken(this.authToken)));            // This works.            //channel = new Channel(host, ChannelCredentials.Insecure);            kvClient = new KV.KVClient(channel);        }        void Authenticate()        {            authClient = new Auth.AuthClient(new Channel(host,ChannelCredentials.Insecure));            var authRes = authClient.Authenticate(new AuthenticateRequest            {                Name = username,                Password = password            });            this.authToken = authRes.Token;        }使用authenticate()方法,我能够从 etcd 服务器获取令牌,但无法找到在后续调用(Get、Put 等)中使用相同令牌的方法。
查看完整描述

1 回答

?
慕斯王

TA贡献1864条经验 获得超2个赞

我通过参考此处的REST api 文档解决了这个问题。

添加私有属性。

Metadata headers;

更新Autheticate()以添加身份验证标头。

headers = new Metadata();
headers.Add("Authorization", authToken);

更新Get()以传递标头。

var rangeResponse = kvClient.Range(rangeRequest, headers);


查看完整回答
反对 回复 2021-07-10
  • 1 回答
  • 0 关注
  • 300 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信