Код: Выделить всё
try
{
string activeProfile = configuration["ActiveProfile"];
string account = configuration[$"Snowflake:{activeProfile}:Account"];
string user = configuration[$"Snowflake:{activeProfile}:User"];
string database = configuration[$"Snowflake:{activeProfile}:Database"];
string warehouse = configuration[$"Snowflake:{activeProfile}:Warehouse"];
string schema = configuration[$"Snowflake:{activeProfile}:Schema"];
string role = configuration[$"Snowflake:{activeProfile}:Role"];
string privateKeyFilePath = "pathtoprivatekey"; // Path to your private key
// Load proxy settings
string proxyHosthttps = configuration[$"Snowflake:Proxy:HTTPS:Host"];
string proxyPorthttps = configuration[$"Snowflake:Proxy:HTTPS:Port"];
string proxyHosthttp = configuration[$"Snowflake:Proxy:HTTP:Host"];
string proxyPorthttp = configuration[$"Snowflake:Proxy:HTTP:Port"];
// Set environment variables for proxy
Environment.SetEnvironmentVariable("HTTP_PROXY", $"http://{proxyHosthttp}:{proxyPorthttp}");
Environment.SetEnvironmentVariable("HTTPS_PROXY", $"http://{proxyHosthttps}:{proxyPorthttps}");
// Load the private key from the file and remove headers, footers, and line breaks
string privateKeyText = File.ReadAllText(privateKeyFilePath)
.Replace("-----BEGIN PRIVATE KEY-----", "")
.Replace("-----END PRIVATE KEY-----", "")
.Replace("\n", "")
.Replace("\r", "")
.Trim(); // Remove any extraneous whitespace
// Reformat the private key with `\n` every 64 characters
StringBuilder formattedPrivateKey = new StringBuilder();
for (int i = 0; i < privateKeyText.Length; i += 64)
{
int chunkSize = Math.Min(64, privateKeyText.Length - i);
formattedPrivateKey.Append(privateKeyText.Substring(i, chunkSize));
formattedPrivateKey.Append("\\n"); // Add newline escape sequence for each 64-char chunk
}
// Handle trailing `=` signs (double any that are present)
string privateKeyFinal = formattedPrivateKey.ToString();
if (privateKeyFinal.EndsWith("="))
{
privateKeyFinal = privateKeyFinal.Replace("==", "====").Replace("=", "==");
}
privateKeyFinal = privateKeyFinal.TrimEnd('\\', 'n'); // Remove trailing \n
// Create the connection string with key-pair authentication (no password here)
var connectionString = $"scheme=https;account={account};user={user};db={database};schema={schema};role={role};warehouse={warehouse};authenticator=snowflake_jwt;PRIVATE_KEY={privateKeyFinal}";
// Create the SnowflakeDbConnection object
using (var _connection = new SnowflakeDbConnection { ConnectionString = connectionString })
{
// Open the connection
_connection.Open();
Console.WriteLine("Successfully connected to Snowflake using key pair authentication.");
}
}
catch (Exception ex)
{
// Print the error message
Console.WriteLine($"Failed to open connection: {ex.Message}\n{ex.StackTrace}");
}
Сообщение об ошибке:
Snowflake.Data.Client.SnowflakeDbException: 'Ошибка: не удалось прочитать закрытый ключ со значением, переданным в строке подключения. \n Ошибка: неправильное значение закрытого ключа или формат закрытого ключа: используйте «\n» для новых строк и удвойте знак равенства. SqlState: , VendorCode: 270052, QueryId: '
Я пробовал разные способы форматирования закрытого ключа, но мне ничего не помогло.
Подробнее здесь: https://stackoverflow.com/questions/791 ... tion-techn
Мобильная версия