2015-07-20 71 views
0

我正在尝试使用Neo4jClient事务支持(Neo4jClient 1.1.0-Tx00008)的最新预发行版。我尝试了最简单的例子,但是当事务完成时(Neo4j Community 2.2.2或2.3),似乎总是会出现认证错误。下面的代码:Neo4jClient交易 - 交易完成时的验证错误

 var graphClient = new GraphClient(new Uri("http://user:[email protected]: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> 

不使用任何交易范围内,节点创建正常。我已经查看了源代码中的单元测试,但没有看到如何调用它的任何区别。

回答

2

你将不得不使用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(); 

这将让所有的交易使用正确的验证标题。感谢您在Github上提高它 - 我们可以考虑让它得到正确解决!

(由 - 代码全部来自:http://geekswithblogs.net/cskardon/archive/2015/03/27/upgrading-your-neo4j-from-xxx-to-2.2.0ndashhaving-authorization-trouble.aspx