Neo4jClient 交易 - 交易完成时出现身份验证错误
Neo4jClient Transactions - Auth Error on Transaction Completion
我正在尝试使用 Neo4jClient 事务支持的最新预发布版本 (Neo4jClient 1.1.0-Tx00008)。我尝试了最简单的示例,但似乎总是在交易完成时收到身份验证错误(Neo4j Community 2.2.2 或 2.3)。以下代码:
var graphClient = new GraphClient(new Uri("http://user:password@localhost:7474/db/data"));
graphClient.Connect();
using (var transaction = graphClient.BeginTransaction())
{
graphClient.Cypher.Create("(n:TestNode {Name:'TestNode2'})").ExecuteWithoutResults();
graphClient.Cypher.Create("(n:TestNode {Name:'TestNode3'})").ExecuteWithoutResults();
transaction.Commit();
}
调用事务提交时总是导致此错误,或者如果我排除 transaction.Commit()
并允许范围结束:
Received an unexpected HTTP status when executing the request.
The response status was: 401 Unauthorized
The response from Neo4j (which might include useful detail!) was: <html>
<head><title>401 Authorization Required</title></head>
<body bgcolor="white">
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx</center>
</body>
</html>
在不使用任何事务范围的情况下,可以很好地创建节点。我查看了源代码中的单元测试,但没有发现调用方式有任何区别。
恐怕您目前必须使用 AuthWrapper,它看起来像:
private class HttpClientAuthWrapper : IHttpClient
{
private readonly AuthenticationHeaderValue _authenticationHeader;
private readonly HttpClient _client;
public HttpClientAuthWrapper(string username, string password)
{
_client = new HttpClient();
if (string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(password))
return;
var encoded = Encoding.ASCII.GetBytes(string.Format("{0}:{1}", username, password));
_authenticationHeader = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(encoded));
}
public Task<HttpResponseMessage> SendAsync(HttpRequestMessage request)
{
if(_authenticationHeader != null)
request.Headers.Authorization = _authenticationHeader;
return _client.SendAsync(request);
}
}
然后像这样使用:
var client = new GraphClient(
new Uri("http://localhost.:7474/db/data"),
new HttpClientAuthWrapper("user", "pass#")
);
client.Connect();
这将使所有交易使用正确的身份验证 header。也感谢您在 Github 上提出问题 - 我们可以考虑妥善解决它!
我正在尝试使用 Neo4jClient 事务支持的最新预发布版本 (Neo4jClient 1.1.0-Tx00008)。我尝试了最简单的示例,但似乎总是在交易完成时收到身份验证错误(Neo4j Community 2.2.2 或 2.3)。以下代码:
var graphClient = new GraphClient(new Uri("http://user:password@localhost:7474/db/data"));
graphClient.Connect();
using (var transaction = graphClient.BeginTransaction())
{
graphClient.Cypher.Create("(n:TestNode {Name:'TestNode2'})").ExecuteWithoutResults();
graphClient.Cypher.Create("(n:TestNode {Name:'TestNode3'})").ExecuteWithoutResults();
transaction.Commit();
}
调用事务提交时总是导致此错误,或者如果我排除 transaction.Commit()
并允许范围结束:
Received an unexpected HTTP status when executing the request.
The response status was: 401 Unauthorized
The response from Neo4j (which might include useful detail!) was: <html>
<head><title>401 Authorization Required</title></head>
<body bgcolor="white">
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx</center>
</body>
</html>
在不使用任何事务范围的情况下,可以很好地创建节点。我查看了源代码中的单元测试,但没有发现调用方式有任何区别。
恐怕您目前必须使用 AuthWrapper,它看起来像:
private class HttpClientAuthWrapper : IHttpClient
{
private readonly AuthenticationHeaderValue _authenticationHeader;
private readonly HttpClient _client;
public HttpClientAuthWrapper(string username, string password)
{
_client = new HttpClient();
if (string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(password))
return;
var encoded = Encoding.ASCII.GetBytes(string.Format("{0}:{1}", username, password));
_authenticationHeader = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(encoded));
}
public Task<HttpResponseMessage> SendAsync(HttpRequestMessage request)
{
if(_authenticationHeader != null)
request.Headers.Authorization = _authenticationHeader;
return _client.SendAsync(request);
}
}
然后像这样使用:
var client = new GraphClient(
new Uri("http://localhost.:7474/db/data"),
new HttpClientAuthWrapper("user", "pass#")
);
client.Connect();
这将使所有交易使用正确的身份验证 header。也感谢您在 Github 上提出问题 - 我们可以考虑妥善解决它!