使用 nest client 7.2.1 连接到 azure 中的弹性数据库内部负载均衡器。
异常:无法为警报事件创建数据库索引。从 HEAD 上的不成功 (401) 低级别调用构建的无效 NEST 响应:/ # 此 API 调用的审计跟踪:- [1] BadResponse:节点:http:// 192.168.0.4:9200/ Exception: PipelineException Took: 00:00:00.0506796 # OriginalException: Elasticsearch.Net.ElasticsearchClientException: 无法通过指定节点进行身份验证。尝试验证您的凭据或检查您的 Shield 配置。调用:状态代码 401 来自:HEAD / ---> Elasticsearch.Net.PipelineException:无法通过指定节点进行身份验证。尝试验证您的凭据或检查您的 Shield 配置。在 Elasticsearch.Net.RequestPipeline.ThrowBadAuthPipelineExceptionWhenNeeded(IApiCallDetails 详细信息,IElasticsearchResponse 响应) path, PostData data, IRequestParameters requestParameters) # Request: # Response: Unsuccessful (401) low level call on PUT: /asx-sleepquality # 此 API 调用的审计跟踪: - [1] BadResponse: Node:
http://192.168 .0.4:9200/Exception: PipelineException Took: 00:00:00.4294201 # OriginalException: Elasticsearch.Net.ElasticsearchClientException: 无法通过指定节点进行身份验证。尝试验证您的凭据或检查您的 Shield 配置。调用:状态代码 401 来自:PUT /asx-sleepquality。 ServerError:类型:security_exception 原因:“缺少 REST 请求的身份验证凭据 [/asx-sleepquality]” ---> Elasticsearch.Net.PipelineException:无法使用指定节点进行身份验证。尝试验证您的凭据或检查您的 Shield 配置。在 Elasticsearch.Net.RequestPipeline.ThrowBadAuthPipelineExceptionWhenNeeded(IApiCallDetails 详细信息,IElasticsearchResponse 响应) path, PostData 数据, IRequestParameters requestParameters) # Request: # Response:
1.Request[TResponse](HttpMethod method, String path, PostData data, IRequestParameters requestParameters) --- End of inner exception stack trace --- # Audit exception in step 1 BadResponse: Elasticsearch.Net.PipelineException: Could not authenticate with the specified node. Try verifying your credentials or check your Shield configuration. at Elasticsearch.Net.RequestPipeline.ThrowBadAuthPipelineExceptionWhenNeeded(IApiCallDetails details, IElasticsearchResponse response) at Elasticsearch.Net.RequestPipeline.CallElasticsearch[TResponse](RequestData requestData) at Elasticsearch.Net.Transport
以下代码解决了我的问题
根据错误消息,您正在尝试向启用了安全性的 Elasticsearch 集群发出请求
您需要在中提供身份验证凭据以供客户端使用
const string Url = "http://192.168.0.4:9200"; var settings = new ConnectionSettings(new Uri(Url)); ElasticSearchClient = new ElasticClient(settings); var indexes = GetAsxIndexes(); var response = ElasticSearchClient.Ping().DebugInformation;
//Reading the configuration from appsettings.json
var elasticConfig = hostContext.Configuration.GetSection("ElasticSearch");
// 1. Register NEST ElasticClient
var settings = new ConnectionSettings(new Uri(elasticConfig["Uri"]))
.BasicAuthentication(elasticConfig["UserName"], elasticConfig["Password"])
.DefaultIndex(elasticConfig["DefaultIndex"]);
settings.PrettyJson();
var client = new ElasticClient(settings);
启用细粒度访问控制时出现错误 401。使用 AwsSigV4HttpConnection 签署请求对我有用。 参考资料:https://opensearch.org/blog/aws-sigv4-support-for-clients/