Search code examples
c#.netneo4jneo4jclient

neo4jclient fails to authenticate against neo4j database when TransactionalGraphClient is used


The following C# code implementing neo4jclient connects to the database correctly:

static readonly string dbConnection = "http://user:pass@localhost:7474/db/data";
GraphClient neoClient = new GraphClient(new Uri(dbConnection));

However the same code fails when the client is cast as transactional.

static readonly string dbConnection = "http://user:pass@localhost:7474/db/data";
ITransactionalGraphClient neoClient = new GraphClient(new Uri(dbConnection));

with the error

The response status was: 401 Unauthorized

The response from Neo4j (which might include useful detail!) was: {
  "errors" : [ {
    "code" : "Neo.ClientError.Security.Unauthorized",
    "message" : "No authentication header supplied."
  } ]
}

I need to use transactions, is there a reason for this behavior or a way to get around it?

UPDATE: I've discovered that the initial database connection actually does work. The exception is thrown from my first Cypher query, so I've posted that query here as well in case that's the problem.

public void AddNode(NodeClass node)
{
    using (var transaction = neoClient.BeginTransaction())
    {
        var paramaters = new
        {
            Id = node.Id,
            Name = node.Name,
            ...
        };

        neoClient.Cypher
           .Create("(x:NodeLabel {paramaters})")
           .WithParam("paramaters", paramaters)
           .ExecuteWithoutResults();

        transaction.Commit();
    }
}

Solution

  • If you change your connection to be:

    new GraphClient(new Uri("http://localhost:7474/db/data"), "user", "pass");

    you will connect and execute your queries absolutely fine.

    In terms of that case not being marked as fixed - that's mainly down to the fact that I usually wait for the bug reporter to mark it as so, but I probably should have a cleanup - in fact I have been, but haven't got there yet.

    From a POV of the documentation being lacking - again I can only apologise. This is mostly down to the fact that unfortunately I have to work to live, and sometimes I feel I just want to relax when I get home :-o