C#-etcd GRPC客户端基本身份验证
我正在尝试为etcd v3+实现一个C#GRPC客户端。我能够通过无身份验证和通道ssl身份验证进行连接。但是,我也在尝试找出基本的身份验证机制。以下是我到目前为止的实现C#-etcd GRPC客户端基本身份验证,c#,.net,.net-core,grpc,etcd,C#,.net,.net Core,Grpc,Etcd,我正在尝试为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.Compi
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;
}
public string Get(string key)
{
try
{
var rangeRequest = new RangeRequest { Key = ByteString.CopyFromUtf8(key) };
var rangeResponse = kvClient.Range(rangeRequest);
if (rangeResponse.Count != 0)
{
return rangeResponse.Kvs[0].Value.ToStringUtf8().Trim();
}
}
catch (Exception ex)
{
}
return String.Empty;
}
}
}
使用authenticate()方法,我能够从etcd服务器获取令牌,但无法找到在后续调用(get、Put等)中使用相同令牌的方法
可以找到用于生成客户端代码的Protobuf文档
更新:
如果有人想看看完整的源代码,下面是我通过参考RESTAPI文档解决的问题 添加一个私有属性
Metadata headers;
更新了authoticate()
以添加auth标头
headers = new Metadata();
headers.Add("Authorization", authToken);
更新了Get()
以传递标题
var rangeResponse = kvClient.Range(rangeRequest, headers);