A nice class to work with Azure Management Certificates

I’ve been using management certificates every now and then, where I always needed a class for reading it out of a publishsettings file and using for the service channel or the WebRequest. I decided to make it look nice :) It bases on the Post Programmatically Installing and Using Your Management Certificate with the New .publishsettings File.

using System;
using System.IO;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Xml.Linq;

namespace YourNamespace.Adapter.Azure
{
    /// <summary>
    /// Class for managing Azure Management Certificates.
    /// </summary>
    public class ManagementCertificate
    {
        private const string PublishProfileTag = "PublishProfile";
        private const string SubscriptionTag = "Subscription";
        private const string ManagementCertificateAttribute = "ManagementCertificate";
        private const string IdAttribute = "Id";

        private readonly string _publishSettingsFilePath;

        /// <summary>
        /// Initializes a new instance of the <see cref="ManagementCertificate"/> class.
        /// </summary>
        /// <param name="publishSettingsFilePath"> The publish settings file path.</param>;
        public ManagementCertificate(string publishSettingsFilePath)
        {
            _publishSettingsFilePath = publishSettingsFilePath;
        }

        /// <summary>
        /// Gets the Azure Management Certificate.
        /// </summary>
        public X509Certificate2 Certificate
        {
            get
            {
                var base64Certificate =
                    XDoc.Descendants(PublishProfileTag)
                        .Single()
                        .Descendants(SubscriptionTag)
                        .Single()
                        .Attribute(ManagementCertificateAttribute)
                        .Value;

                return new X509Certificate2(Convert.FromBase64String(base64Certificate));
            }
        }

        /// <summary>
        /// Gets the subscription id.
        /// </summary>
        public string SubscriptionId
        {
            get
            {
                return XDoc.Descendants(SubscriptionTag).First().Attribute(IdAttribute).Value;
            }
        }

        /// <summary>
        /// Gets the thumbprint.
        /// </summary>
        public string Thumbprint
        {
            get
            {
                return Certificate.Thumbprint;
            }
        }

        private XDocument XDoc
        {
            get
            {
                return XDocument.Load(_publishSettingsFilePath);
            }
        }

        /// <summary>
        /// Saves the certificate.
        /// </summary>
        /// <param name="certFilePath">The cert file path.</param>
        /// <param name="serializedCert">if set to <c>true</c> [serialized cert].</param>
        public void SaveCertificate(string certFilePath, bool serializedCert = false)
        {
            var certificateFileContent = new StringBuilder();
            certificateFileContent.AppendLine("-----BEGIN CERTIFICATE-----");
            certificateFileContent.AppendLine(Convert.ToBase64String(Certificate.Export(serializedCert ? X509ContentType.SerializedCert : X509ContentType.Cert), Base64FormattingOptions.InsertLineBreaks));
            certificateFileContent.AppendLine("-----END CERTIFICATE-----");
            File.WriteAllText(certFilePath, certificateFileContent.ToString());
        }
    }
}
Advertisements

About Tamas Nemeth

Husband and proud father of two daughters in Nürnberg. I'm working as a Senior Software Developer and an enthusiastic Clean-Coder. I spend most of my free time with my family (playing, hiking, etc...). I also play table-tennis and badminton sometimes...
This entry was posted in Technical Interest and tagged , , . Bookmark the permalink.

One Response to A nice class to work with Azure Management Certificates

  1. Pingback: Configuring WCF Client to use the Windows Azure Service Management Library | Tamas' Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s