3

I know that you can get the Connection string using this:

 <add key="StorageConnectionString" value="DefaultEndpointsProtocol=https;AccountName=mystorage;AccountKey=MY_ACCOUNT_KEY" />

And then retrieve it using this:

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));

And I know how to build a connection string using SqlConnectionStringBuilder:

System.Data.SqlClient.SqlConnectionStringBuilder builder = new System.Data.SqlClient.SqlConnectionStringBuilder();
builder["DefaultEndpointsProtocol"] = "https";
builder["AccountName"] = "mystorage";
builder["AccountKey"] = "MY_ACCOUNT_KEY";
string conString = builder.ConnectionString;

However apparently this doesn't work for Storage Connection Strings. It says SqlConnectionStringBuilder doesn't support DefaultEndpointsProtocol or any of the 3 keys I have specified. How can I make a string out of these keys?

1
  • First of all, Azure Storage is not the same as SQL Azure. Now, presuming that you want to connect to SQL Azure and not the Azure Storage, then there are a lot of ways of achieving this, have you seen this link connectionstrings.com/sql-azure ? Commented Sep 6, 2015 at 16:29

2 Answers 2

9

Answer

Use the CloudStorageAccount constructor that takes a StorageCredentials object. Then use the ToString(boolean) overload to get the connection string.

Demo with Fiddle https://dotnetfiddle.net/ReWDqL

var accountName = "myAccount";
var keyValue = "c3RyaW5nIGxlbmd0aCB2YWxpZA==";
var useHttps = true;
var exportSecrets = true;

var storageCredentials = new StorageCredentials(accountName, keyValue);
var storageAccount = new CloudStorageAccount(storageCredentials, useHttps);
var connString = storageAccount.ToString(exportSecrets);

MSDN Documentation

StorageCredentials Constructor (String, String). Initializes a new instance of the StorageCredentials class with the specified account name and key value.

CloudStorageAccount Constructor (StorageCredentials, Boolean). Initializes a new instance of the CloudStorageAccount class using the specified credentials, and specifies whether to use HTTP or HTTPS to connect to the storage services.

CloudStorageAccount.ToString Method (Boolean). Returns a connection string for the storage account, optionally with sensitive data.

Sign up to request clarification or add additional context in comments.

Comments

5

Not sure why don't you use the first option that you have working(Maybe you want to keep them separate in your config file), but in any case you can just concat them together to build the string:

var storageConnectionString = String.format("DefaultEndpointsProtocol={0};AccountName={1};AccountKey={2}", "https", "MyAccountName","MyAccountKey");

CloudStorageAccount.Parse(storageConnectionString);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.