CreateManager using credentials from app.config

I have an .net app where I have my connection info and bucket credentials in my app.config

I am trying to get hold of the ClusterManager, using the credentials from my config file

I can do the following to get a bucket without entering credentials.

	ClusterHelper.Initialize("couchbaseClients/couchbase");
	var cluster = ClusterHelper.Get();
	var bucket = cluster.OpenBucket("mybucket");

I can use the credentials for this bucket to get the ClusterManager (my bucket name is the same as my bucket username)

	ClusterHelper.Initialize("couchbaseClients/couchbase");
	var cluster = ClusterHelper.Get();
	var manager = cluster.CreateManager("mybucket", "mybucketpwd");

But I can’t find a way to get the manager without programatically providing the password to createmanager, even though the CreateManager method does have a parameterless call.

	ClusterHelper.Initialize("couchbaseClients/couchbase");
	var cluster = ClusterHelper.Get();
	var manager = cluster.CreateManager();

This throws an exception

	System.Security.Authentication.AuthenticationException occurred
	  HResult=0x80131501
	  Message=No credentials found.
	  Source=Couchbase.NetClient

Is there a way of achieving this? I dont have a problem providing the bucket name (or username) as a parameter, since it’s a constant in my code. I guess I could traverse the config xml and get the password I need, but it seems like a clumsy solution.

I’m using Couchbase 5.0.0 and CouchbaseNetClient 2.5.2
and my config section looks like this

  <couchbaseClients>
    <couchbase useSsl="false">
      <servers>
        <add uri="http://localhost:8091/pools"></add>
      </servers>
      <buckets>
        <add name="mybucket" password="mybucketpwd" />
		<!--etc-->
      </buckets>
    </couchbase>
  </couchbaseClients>

Thanks in advance

@Columbo

You should try switching to the new authentication method, instead of the old bucket password method. To do that, drop “password” from your bucket. Then add and tags within the section:

<couchbaseClients>
    <couchbase useSsl="false">
      <username>username</username>
      <password>password</password>
      <servers>
        <add uri="http://localhost:8091/pools"></add>
      </servers>
      <buckets>
        <add name="mybucket" />
		<!--etc-->
      </buckets>
    </couchbase>
  </couchbaseClients>

Make sure the user you supply has been granted access to your bucket as well as to any cluster management functions required.

3 Likes

Thanks for that, I wasn’t aware of this change to the config - we’ve only just upgraded from 4.5.1 to 5.0.0 and hadn’t paid attention to changes in the client’s authentication method. We had 3 buckets each with their own user and pwd, but I don’t see why we shouldn’t just have one user for all buckets.

I’m just trying to test this out, but am currently having a little trouble adding in the and elements to my config - having added them just like your config snippet, and using ClusterHelper.Initialize(“couchbaseClients/couchbase”) I’m getting a System.Configuration.ConfigurationErrorsException - “Property ‘username’ is not a ConfigurationElement.” I can’t see why this is a problem, I’ve tried with 2.5.2 and 2.5.3 , the source in Git for the CouchbaseClientSection seems to define the config element for username just as I’d expect. Oh well, I’ve probably done something dumb here, hope I’ll figure it out eventually.

Hi @Columbo

The username and password fields are properties of the root configuration section, not sub-nodes. This example config works for me connecting to a 5.0 Couchbase cluster using .NET client 2.5.3:

<configSections>
    <section name="couchbase" type="Couchbase.Configuration.Client.Providers.CouchbaseClientSection, Couchbase.NetClient" />
</configSections>
<couchbase username="mike" password="secure123">
    <servers>
        <add uri="http://10.112.170.101:8091" />
    </servers>
    <buckets>
        <add name="travel-sample" />
    </buckets>
</couchbase>

With this code:

class Program
{
    static void Main()
    {
        using (var cluster = new Cluster("couchbase"))
        {
           var manager = cluster.CreateManager();
           var result = manager.ListBuckets();
        }
    }
}
1 Like

That was it, I had been staring at that problem longer than I’d care to admit - thank you!

1 Like

@Columbo

Sorry, my mistake. I guess I should have looked that up instead of going from memory. :slight_smile:

That’s ok, I should really have learned to read a config section handler by now

Hi Mike, Could you please help with the below

how to configure CertificateFactory with cert details in config?