/// <summary>
        /// Get the Integrity key
        /// </summary>
        /// <returns>key as string.</returns>
        public string GetCurrentVaultChannelIntegrityKey()
        {
            VaultExtendedInfoResource extendedInformation = null;

            try
            {
                extendedInformation = this.GetExtendedInfo();
            }
            catch (Exception exception)
            {
                CloudException cloudException = exception as CloudException;

                if (cloudException != null && cloudException.Response != null &&
                    !string.IsNullOrEmpty(cloudException.Response.Content))
                {
                    rpError.Error error = JsonConvert.DeserializeObject <rpError.Error>(
                        cloudException.Response.Content,
                        new JsonSerializerSettings {
                        DateFormatHandling = DateFormatHandling.MicrosoftDateFormat
                    });

                    if (error.ErrorCode.Equals(
                            RpErrorCode.ResourceExtendedInfoNotFound.ToString(),
                            StringComparison.InvariantCultureIgnoreCase))
                    {
                        extendedInformation = new VaultExtendedInfoResource();
                    }
                }
            }

            if (null == extendedInformation)
            {
                extendedInformation = this.CreateVaultExtendedInformation();
            }
            else
            {
                if (!extendedInformation.Algorithm.Equals(
                        CryptoAlgorithm.None.ToString(),
                        StringComparison.InvariantCultureIgnoreCase))
                {
                    // In case this condition is true that means the credential was first generated in portal
                    // and hence can not be fetched here.
                    throw new CloudException(Resources.VaultSettingsGenerationUnSupported);
                }
            }

            return(extendedInformation.IntegrityKey);
        }
        /// <summary>
        /// Get the Integrity key
        /// </summary>
        /// <returns>key as string.</returns>
        private async Task <string> GetChannelIntegrityKey()
        {
            ResourceExtendedInformation extendedInformation = null;

            try
            {
                extendedInformation = await this.GetExtendedInfo();
            }
            catch (Exception exception)
            {
                CloudException cloudException = exception as CloudException;

                if (cloudException != null && cloudException.Response != null && !string.IsNullOrEmpty(cloudException.Response.Content))
                {
                    JavaScriptSerializer serializer = new JavaScriptSerializer();
                    rpError.Error        error      = serializer.Deserialize <rpError.Error>(cloudException.Response.Content);

                    if (error.ErrorCode.Equals(RpErrorCode.ResourceExtendedInfoNotFound.ToString(), StringComparison.InvariantCultureIgnoreCase))
                    {
                        extendedInformation = new ResourceExtendedInformation();
                    }
                }
            }

            if (null == extendedInformation.Properties)
            {
                extendedInformation = this.CreateVaultExtendedInformation();
            }
            else
            {
                if (!extendedInformation.Properties.Algorithm.Equals(CryptoAlgorithm.None.ToString(), StringComparison.InvariantCultureIgnoreCase))
                {
                    // In case this condition is true that means the credential was first generated in portal
                    // and hence can not be fetched here.
                    throw new CloudException(Resources.VaultSettingsGenerationUnSupported);
                }
            }

            return(extendedInformation.Properties.IntegrityKey);
        }
Ejemplo n.º 3
0
        /// <summary>
        ///     Get extendVault Info.
        /// </summary>
        /// <param name="vaultResourceGroupName">Vault ResourceGroup Name</param>
        /// <param name="vaultName">Vault Name</param>
        /// <returns>VaultExtendedInfo Resource Object</returns>
        public VaultExtendedInfoResource GetVaultExtendedInfo(String vaultResourceGroupName, String vaultName)
        {
            VaultExtendedInfoResource extendedInformation = null;

            try
            {
                extendedInformation = this.recoveryServicesVaultClient
                                      .VaultExtendedInfo
                                      .GetWithHttpMessagesAsync(vaultResourceGroupName, vaultName, this.GetRequestHeaders(false))
                                      .GetAwaiter()
                                      .GetResult()
                                      .Body;
            }
            catch (Exception exception)
            {
                CloudException cloudException = exception as CloudException;

                if (!string.IsNullOrEmpty(cloudException?.Response?.Content))
                {
                    rpError.Error error = JsonConvert.DeserializeObject <rpError.Error>(cloudException.Response.Content);

                    if (error.ErrorCode.Equals(
                            RpErrorCode.ResourceExtendedInfoNotFound.ToString(),
                            StringComparison.InvariantCultureIgnoreCase))
                    {
                        extendedInformation = new VaultExtendedInfoResource();
                        extendedInformation.IntegrityKey = Utilities.GenerateRandomKey(128);
                        extendedInformation.Algorithm    = CryptoAlgorithm.None.ToString();
                        extendedInformation = this.recoveryServicesVaultClient.VaultExtendedInfo.CreateOrUpdateWithHttpMessagesAsync(
                            vaultResourceGroupName,
                            vaultName,
                            extendedInformation,
                            GetRequestHeaders(false)).Result.Body;
                    }
                }
            }

            return(extendedInformation);
        }