2015-07-20 123 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