5
votes

Given a blob storage connection string such as:

DefaultEndpointsProtocol=https;AccountName=foo;AccountKey=bar;EndpointSuffix=core.windows.net

Is there a known Microsoft object that this can be converted / deserialized into? I don't want to actually parse the string, but I need to extract the AccountName and AccountKey from the entire connection string, which I have as a string.

To pre-empt possible "Why do you want to do this?" questions... I have an existing class that requires the connection string to be injected as a string. To avoid breaking changes, I can't alter that. But I do need to add some methods in this class that need the AccountName and AccountKey as individual items.

Thanks!

4

4 Answers

6
votes

If you install Microsoft.Azure.Storage.Common, you can extract several bits of your connection string programmatically, without parsing the connection string yourself.

For example (with actual info obfuscated):

using System;
using Microsoft.Azure.Storage;

namespace dotnet_connectionstring
{
    class Program
    {
        static void Main(string[] args)
        {
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=youraccount;AccountKey=yourkey==;EndpointSuffix=core.windows.net");
            Console.WriteLine(storageAccount.BlobEndpoint);
            Console.WriteLine("---");
            Console.WriteLine(storageAccount.BlobStorageUri);
            Console.WriteLine("---");
            Console.WriteLine(storageAccount.Credentials.AccountName);
            Console.WriteLine("---");
            Console.WriteLine(storageAccount.Credentials.ExportBase64EncodedKey());
        }
    }
}

This gives output something like:

https://youraccount.blob.core.windows.net/
---
Primary = 'https://youraccount.blob.core.windows.net/'; Secondary = 'https://youraccount-secondary.blob.core.windows.net/'
---
youraccount
---
yourkey==
5
votes

There are no classes that I know of that do this, but it wouldn't be that hard to change it into a dictionary. Example below.

        string connString = "DefaultEndpointsProtocol=https;AccountName=foo;AccountKey=bar;EndpointSuffix=core.windows.net";

        var connStringArray = connString.Split(';');

        var dictionary = new Dictionary<string, string>();

        foreach (var item in connStringArray)
        {
            var itemKeyValue = item.Split('=');
            dictionary.Add(itemKeyValue[0], itemKeyValue[1]);
        }

Then you could access the values you need using this.

dictionary["AccountName"]
dictionary["AccountKey"]
2
votes

For this purpose, we have CloudStorageAccount type from Microsoft.WindowsAzure.Storage assembly.

CloudStorageAccount sa = CloudStorageAccount.Parse(connString);
1
votes

The answer of @David Makogon is certainly the most elegant but the package Microsoft.Azure.Storage.Common is deprecated (as stated in comments). Based on @Patrick Mcvay answer (which is just a bit bugged as there might be '=' in the connection string values), an easy way to parse a connection string would be:

var parsedConnectionString = new Dictionary<string, string>();
foreach (var item in ConnectionString.Split(';'))
{
    var idx = item.IndexOf('=');
    parsedConnectionString[item.Substring(0, idx)] =
        item.Substring(idx + 1, item.Length - idx - 1);
}