Username and password based authentication

Note

The article is being updated.

Below are examples of the code for authentication based on a username and token in different YDB SDKs.

auto driverConfig = NYdb::TDriverConfig()
   .SetEndpoint(endpoint)
   .SetDatabase(database)
   .SetCredentialsProviderFactory(NYdb::CreateLoginCredentialsProviderFactory({
      .User = "user",
      .Password = "password",
   }));

NYdb::TDriver driver(driverConfig);

You can pass the username and password in the connection string. For example:

"grpcs://login:password@localohost:2135/local"

You can also explicitly pass them using the ydb.WithStaticCredentials parameter:

{% include auth-static-with-native %}

You can pass the username and password in the connection string. For example:

{% include auth-static-database-sql %}

You can also explicitly pass the username and password at driver initialization via a connector using the ydb.WithStaticCredentials parameter:

{% include auth-static-with-database-sql %}

public void work(String connectionString, String username, String password) {
    AuthProvider authProvider = new StaticCredentials(username, password);

    GrpcTransport transport = GrpcTransport.forConnectionString(connectionString)
            .withAuthProvider(authProvider)
            .build());

    TableClient tableClient = TableClient.newClient(transport).build();

    doWork(tableClient);

    tableClient.close();
    transport.close();
}

{% include auth-static %}

{% include auth-static %}

{% include auth-static %}

using Ydb.Sdk;
using Ydb.Sdk.Auth;

const string endpoint = "grpc://localhost:2136";
const string database = "/local";

var config = new DriverConfig(
    endpoint: endpoint, // Database endpoint, "grpcs://host:port"
    database: database, // Full database path
    credentials: new StaticCredentialsProvider(user, password)
);

await using var driver = await Driver.CreateInitialized(config);
<?php

use YdbPlatform\Ydb\Ydb;
use YdbPlatform\Ydb\Auth\Implement\StaticAuthentication;

$config = [

    // Database path
    'database'    => '/local',

    // Database endpoint
    'endpoint'    => 'localhost:2136',

    // Auto discovery (dedicated server only)
    'discovery'   => false,

    // IAM config
    'iam_config'  => [
        'insecure' => true,
        // 'root_cert_file' => './CA.pem', // Root CA file (uncomment for dedicated server)
    ],

    'credentials' => new StaticAuthentication($user, $password)
];

$ydb = new Ydb($config);