设置安全性后使用 C# 访问弹性搜索
access elastic search using C# after setting up security
我在 this link 之后设置了安全性。这意味着我现在必须在访问时输入用户名和密码:
http://localhost:9200/
假设用户名是 un,pw
是 pw234
。我现在如何在我的 C# 代码中使用它。我试过了:
public static ElasticClient GetClient(string indexName)
{
var node = new Uri(ConfigurationManager.AppSettings["Search-Uri"]);
var settings = new ConnectionSettings(node)
.DefaultIndex(indexName);
settings.ThrowExceptions(alwaysThrow: true); // I like exceptions
settings.PrettyJson(); // Good for DEBUG
settings.RequestTimeout(TimeSpan.FromSeconds(300));
settings.BasicAuthentication("un", "pw234");
return new ElasticClient(settings);
}
我必须使用 BasicAuthentication 吗?请注意,这绝不是生产代码。我永远不会像这样硬编码用户名和密码。谢谢。
基本身份验证是 built-in 用户帐户 (native realm) 可以用来进行身份验证的一种身份验证方案。支持其他 token-based 身份验证服务:
- Token service using
Bearer
(authentication scheme) tokens,基于 OAuth2 规范
- API keys using
ApiKey
(authentication scheme) tokens
通常,对于与 Elasticsearch 交互的应用程序(即 non-users),您可能会使用其中一种基于令牌的身份验证服务。客户端公开配置 API 密钥以在 ConnectionSettings
上进行身份验证
public static ElasticClient GetClient(string indexName)
{
var node = new Uri(ConfigurationManager.AppSettings["Search-Uri"]);
var settings = new ConnectionSettings(node)
.DefaultIndex(indexName)
.ThrowExceptions(alwaysThrow: true) // I like exceptions
.PrettyJson() // Good for DEBUG
.RequestTimeout(TimeSpan.FromSeconds(300))
.ApiKeyAuthentication("<id>", "<api key>");
return new ElasticClient(settings);
}
不记名令牌也可以与客户端一起使用,但是没有公开设置 header 的专用方法,因为有基本身份验证和 api 密钥身份验证,所以 .GlobalHeaders()
方法在 ConnectionSettings
和每个请求的基础上(通过 .RequestConfiguration()
)需要被使用。
除了原生realm,Elasticsearch还有supports the following realms
我在 this link 之后设置了安全性。这意味着我现在必须在访问时输入用户名和密码:
http://localhost:9200/
假设用户名是 un,pw
是 pw234
。我现在如何在我的 C# 代码中使用它。我试过了:
public static ElasticClient GetClient(string indexName)
{
var node = new Uri(ConfigurationManager.AppSettings["Search-Uri"]);
var settings = new ConnectionSettings(node)
.DefaultIndex(indexName);
settings.ThrowExceptions(alwaysThrow: true); // I like exceptions
settings.PrettyJson(); // Good for DEBUG
settings.RequestTimeout(TimeSpan.FromSeconds(300));
settings.BasicAuthentication("un", "pw234");
return new ElasticClient(settings);
}
我必须使用 BasicAuthentication 吗?请注意,这绝不是生产代码。我永远不会像这样硬编码用户名和密码。谢谢。
基本身份验证是 built-in 用户帐户 (native realm) 可以用来进行身份验证的一种身份验证方案。支持其他 token-based 身份验证服务:
- Token service using
Bearer
(authentication scheme) tokens,基于 OAuth2 规范 - API keys using
ApiKey
(authentication scheme) tokens
通常,对于与 Elasticsearch 交互的应用程序(即 non-users),您可能会使用其中一种基于令牌的身份验证服务。客户端公开配置 API 密钥以在 ConnectionSettings
public static ElasticClient GetClient(string indexName)
{
var node = new Uri(ConfigurationManager.AppSettings["Search-Uri"]);
var settings = new ConnectionSettings(node)
.DefaultIndex(indexName)
.ThrowExceptions(alwaysThrow: true) // I like exceptions
.PrettyJson() // Good for DEBUG
.RequestTimeout(TimeSpan.FromSeconds(300))
.ApiKeyAuthentication("<id>", "<api key>");
return new ElasticClient(settings);
}
不记名令牌也可以与客户端一起使用,但是没有公开设置 header 的专用方法,因为有基本身份验证和 api 密钥身份验证,所以 .GlobalHeaders()
方法在 ConnectionSettings
和每个请求的基础上(通过 .RequestConfiguration()
)需要被使用。
除了原生realm,Elasticsearch还有supports the following realms