public async Task CreateSecret(VehiclePolicies policydata) { // Create the content for the Policy data to be stored as Secret Insdata akvdata = new Insdata { Id = policydata.Id, Inscompany = policydata.Inscompany, Policyno = policydata.Policyno, Userid = policydata.Userid, Vehicleno = policydata.Vehicleno }; //Create a JSON String of the Policy data to be stored as Secret string insurancepolicysecret = JsonConvert.SerializeObject(akvdata); byte[] datatoencrypt = System.Text.Encoding.UTF8.GetBytes(insurancepolicysecret); string keyUri = string.Format("https://{0}.vault.azure.net/keys/{1}", _keyVaultName, _keyName); string keyVaultUri = string.Format("https://{0}.vault.azure.net", _keyVaultName); //Encrypt the data before it is stored as a Secret KeyOperationResult result = await _keyVaultClient.EncryptAsync(keyUri, JsonWebKeyEncryptionAlgorithm.RSAOAEP256, datatoencrypt); byte[] encdata = result.Result; string encrypteddata = Convert.ToBase64String(encdata); //Set the Policy Start and Expiry Data to be added as attributes to the secret SecretAttributes attribs = new SecretAttributes { Enabled = true, Expires = DateTime.UtcNow.AddYears(1), NotBefore = DateTime.UtcNow }; IDictionary <string, string> alltags = new Dictionary <string, string> { { "InsuranceCompany", policydata.Inscompany } }; string contentType = "DigitalInsurance"; // Create a Secret with the encrypted Policy data SecretBundle bundle = await _keyVaultClient.SetSecretAsync(keyVaultUri, policydata.Uidname, encrypteddata, alltags, contentType, attribs); string bundlestr = bundle.Value; policydata.Version = bundle.SecretIdentifier.Version; policydata.Lastmod = bundle.Attributes.Updated; policydata.Startdate = bundle.Attributes.NotBefore; policydata.Enddate = bundle.Attributes.Expires; }
public async Task UpdateSecret(VehiclePolicies policydata) { //Create the updated Policy data to be stored as a new version of the Secret Insdata akvdata = new Insdata { Id = policydata.Id, Inscompany = policydata.Inscompany, Policyno = policydata.Policyno, Userid = policydata.Userid, Vehicleno = policydata.Vehicleno }; //Create the JSON String of the updated Policy Object string insurancepolicysecret = JsonConvert.SerializeObject(akvdata); byte[] datatoencrypt = System.Text.Encoding.UTF8.GetBytes(insurancepolicysecret); string keyUri = string.Format("https://{0}.vault.azure.net/keys/{1}", _keyVaultName, _keyName); string keyVaultUri = string.Format("https://{0}.vault.azure.net", _keyVaultName); KeyOperationResult result = null; //Get the metadata from the existing Secret in Key Vault SecretBundle bundle = await _keyVaultClient.GetSecretAsync(keyVaultUri, policydata.Uidname); if (bundle == null) { throw new ApplicationException("Error locating Secret data to update"); //No need to execute the rest of the steps if the Secret cannot be retrieved } SecretAttributes _attribs = bundle.Attributes; string _contentType = bundle.ContentType; IDictionary <string, string> dic = bundle.Tags; //Create the attributes for the updated Secret SecretAttributes attribsNew = new SecretAttributes { Enabled = true, Expires = _attribs.Expires, NotBefore = DateTime.UtcNow }; IDictionary <string, string> alltags = dic; string contentType = _contentType; // Encrypt the updated Secret data result = await _keyVaultClient.EncryptAsync(keyUri, JsonWebKeyEncryptionAlgorithm.RSAOAEP256, datatoencrypt); byte[] encdata = result.Result; string encrypteddata = Convert.ToBase64String(encdata); //Create a new version of the Secret by calling the SetSecret Method, and using the attributes from the previous version of the Secret bundle = await _keyVaultClient.SetSecretAsync(keyVaultUri, policydata.Uidname, encrypteddata, alltags, contentType, attribsNew); string bundlestr = bundle.Value; policydata.Version = bundle.SecretIdentifier.Version; policydata.Lastmod = bundle.Attributes.Updated; policydata.Startdate = bundle.Attributes.NotBefore; policydata.Enddate = bundle.Attributes.Expires; }