|
| 1 | +using System.Security.Cryptography; |
| 2 | +using System.Security.Cryptography.X509Certificates; |
| 3 | +using Azure.Identity; |
| 4 | +using Microsoft.AspNetCore.DataProtection; |
| 5 | +using Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption; |
| 6 | +using Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ConfigurationModel; |
| 7 | + |
| 8 | +namespace DataProtectionConfigurationSample.Snippets; |
| 9 | + |
| 10 | +public static class Program |
| 11 | +{ |
| 12 | + public static void AddDataProtectionProtectKeysWithAzureKeyVault(WebApplicationBuilder builder) |
| 13 | + { |
| 14 | + // <snippet_AddDataProtectionProtectKeysWithAzureKeyVault> |
| 15 | + builder.Services.AddDataProtection() |
| 16 | + .PersistKeysToAzureBlobStorage(new Uri("<blobUriWithSasToken>")) |
| 17 | + .ProtectKeysWithAzureKeyVault(new Uri("<keyIdentifier>"), |
| 18 | + new DefaultAzureCredential()); |
| 19 | + // </snippet_AddDataProtectionProtectKeysWithAzureKeyVault> |
| 20 | + } |
| 21 | + |
| 22 | + public static void AddDataProtectionProtectKeysWithAzureKeyVaultConnectionString(WebApplicationBuilder builder) |
| 23 | + { |
| 24 | + // <snippet_AddDataProtectionProtectKeysWithAzureKeyVaultConnectionString> |
| 25 | + builder.Services.AddDataProtection() |
| 26 | + // This blob must already exist before the application is run |
| 27 | + .PersistKeysToAzureBlobStorage( |
| 28 | + "<storageAccountConnectionString", "<containerName>", "<blobName>") |
| 29 | + // Removing this line below for an initial run will ensure the file is |
| 30 | + // created correctly |
| 31 | + .ProtectKeysWithAzureKeyVault(new Uri("<keyIdentifier>"), |
| 32 | + new DefaultAzureCredential()); |
| 33 | + // </snippet_AddDataProtectionProtectKeysWithAzureKeyVaultConnectionString> |
| 34 | + } |
| 35 | + |
| 36 | + public static void AddDataProtectionPersistKeysToFileSystem(WebApplicationBuilder builder) |
| 37 | + { |
| 38 | + // <snippet_AddDataProtectionPersistKeysToFileSystem> |
| 39 | + builder.Services.AddDataProtection() |
| 40 | + .PersistKeysToFileSystem( |
| 41 | + new DirectoryInfo(@"\\server\share\directory\")); |
| 42 | + // </snippet_AddDataProtectionPersistKeysToFileSystem> |
| 43 | + } |
| 44 | + |
| 45 | + public static void AddDataProtectionPersistKeysToDbContext(WebApplicationBuilder builder) |
| 46 | + { |
| 47 | + // <snippet_AddDataProtectionPersistKeysToDbContext> |
| 48 | + builder.Services.AddDataProtection() |
| 49 | + .PersistKeysToDbContext<SampleDbContext>(); |
| 50 | + // </snippet_AddDataProtectionPersistKeysToDbContext> |
| 51 | + } |
| 52 | + |
| 53 | + public static void AddDataProtectionProtectKeysWithCertificate(WebApplicationBuilder builder) |
| 54 | + { |
| 55 | + // <snippet_AddDataProtectionProtectKeysWithCertificate> |
| 56 | + builder.Services.AddDataProtection() |
| 57 | + .PersistKeysToFileSystem( |
| 58 | + new DirectoryInfo(builder.Configuration["CertificatePath"] ?? |
| 59 | + throw new Exception("No path"))) |
| 60 | + .ProtectKeysWithCertificate( |
| 61 | + builder.Configuration["CertificateThumbprint"] ?? |
| 62 | + throw new Exception("No thumbprint")); |
| 63 | + // </snippet_AddDataProtectionProtectKeysWithCertificate> |
| 64 | + } |
| 65 | + |
| 66 | + public static void AddDataProtectionProtectKeysWithCertificateX509Certificate2(WebApplicationBuilder builder) |
| 67 | + { |
| 68 | + // <snippet_AddDataProtectionProtectKeysWithCertificateX509Certificate2> |
| 69 | + builder.Services.AddDataProtection() |
| 70 | + .PersistKeysToFileSystem(new DirectoryInfo( |
| 71 | + builder.Configuration["CertificatePath"] ?? |
| 72 | + throw new Exception("No path"))) |
| 73 | + .ProtectKeysWithCertificate( |
| 74 | + X509CertificateLoader.LoadCertificateFromFile( |
| 75 | + builder.Configuration["CertificateFileName"] ?? |
| 76 | + throw new Exception("No file name"))); |
| 77 | + // </snippet_AddDataProtectionProtectKeysWithCertificateX509Certificate2> |
| 78 | + } |
| 79 | + |
| 80 | + public static void AddDataProtectionUnprotectKeysWithAnyCertificate(WebApplicationBuilder builder) |
| 81 | + { |
| 82 | + // <snippet_AddDataProtectionUnprotectKeysWithAnyCertificate> |
| 83 | + builder.Services.AddDataProtection() |
| 84 | + .PersistKeysToFileSystem(new DirectoryInfo(@"\\server\share\directory\")) |
| 85 | + .ProtectKeysWithCertificate( |
| 86 | + X509CertificateLoader.LoadCertificateFromFile( |
| 87 | + builder.Configuration["CertificateFileName"] ?? |
| 88 | + throw new Exception("No file name"))) |
| 89 | + .UnprotectKeysWithAnyCertificate( |
| 90 | + X509CertificateLoader.LoadCertificateFromFile( |
| 91 | + builder.Configuration["CertificateFileName1"] ?? |
| 92 | + throw new Exception("No file name 1")), |
| 93 | + X509CertificateLoader.LoadCertificateFromFile( |
| 94 | + builder.Configuration["CertificateFileName2"] ?? |
| 95 | + throw new Exception("No file name 2"))); |
| 96 | + // </snippet_AddDataProtectionUnprotectKeysWithAnyCertificate> |
| 97 | + } |
| 98 | + |
| 99 | + public static void AddDataProtectionSetDefaultKeyLifetime(WebApplicationBuilder builder) |
| 100 | + { |
| 101 | + // <snippet_AddDataProtectionSetDefaultKeyLifetime> |
| 102 | + builder.Services.AddDataProtection() |
| 103 | + .SetDefaultKeyLifetime(TimeSpan.FromDays(14)); |
| 104 | + // </snippet_AddDataProtectionSetDefaultKeyLifetime> |
| 105 | + } |
| 106 | + |
| 107 | + public static void AddDataProtectionSetApplicationName(WebApplicationBuilder builder) |
| 108 | + { |
| 109 | + // <snippet_AddDataProtectionSetApplicationName> |
| 110 | + builder.Services.AddDataProtection() |
| 111 | + .SetApplicationName("<sharedApplicationName>"); |
| 112 | + // </snippet_AddDataProtectionSetApplicationName> |
| 113 | + } |
| 114 | + |
| 115 | + public static void AddDataProtectionDisableAutomaticKeyGeneration(WebApplicationBuilder builder) |
| 116 | + { |
| 117 | + // <snippet_AddDataProtectionDisableAutomaticKeyGeneration> |
| 118 | + builder.Services.AddDataProtection() |
| 119 | + .DisableAutomaticKeyGeneration(); |
| 120 | + // </snippet_AddDataProtectionDisableAutomaticKeyGeneration> |
| 121 | + } |
| 122 | + |
| 123 | + public static void AddDataProtectionUseCryptographicAlgorithms(WebApplicationBuilder builder) |
| 124 | + { |
| 125 | + // <snippet_AddDataProtectionUseCryptographicAlgorithms> |
| 126 | + builder.Services.AddDataProtection() |
| 127 | + .UseCryptographicAlgorithms(new AuthenticatedEncryptorConfiguration |
| 128 | + { |
| 129 | + EncryptionAlgorithm = EncryptionAlgorithm.AES_256_CBC, |
| 130 | + ValidationAlgorithm = ValidationAlgorithm.HMACSHA256 |
| 131 | + }); |
| 132 | + // </snippet_AddDataProtectionUseCryptographicAlgorithms> |
| 133 | + } |
| 134 | + |
| 135 | + public static void AddDataProtectionUseCustomCryptographicAlgorithms(WebApplicationBuilder builder) |
| 136 | + { |
| 137 | + // <snippet_AddDataProtectionUseCustomCryptographicAlgorithms> |
| 138 | + builder.Services.AddDataProtection() |
| 139 | + .UseCustomCryptographicAlgorithms( |
| 140 | + new ManagedAuthenticatedEncryptorConfiguration |
| 141 | + { |
| 142 | + // A type that subclasses SymmetricAlgorithm |
| 143 | + EncryptionAlgorithmType = typeof(Aes), |
| 144 | + |
| 145 | + // Specified in bits |
| 146 | + EncryptionAlgorithmKeySize = 256, |
| 147 | + |
| 148 | + // A type that subclasses KeyedHashAlgorithm |
| 149 | + ValidationAlgorithmType = typeof(HMACSHA256) |
| 150 | + }); |
| 151 | + // </snippet_AddDataProtectionUseCustomCryptographicAlgorithms> |
| 152 | + } |
| 153 | + |
| 154 | +#pragma warning disable CA1416 // Validate platform compatibility |
| 155 | + public static void AddDataProtectionUseCustomCryptographicAlgorithmsCngCbc(WebApplicationBuilder builder) |
| 156 | + { |
| 157 | + // <snippet_AddDataProtectionUseCustomCryptographicAlgorithmsCngCbc> |
| 158 | + builder.Services.AddDataProtection() |
| 159 | + .UseCustomCryptographicAlgorithms( |
| 160 | + new CngCbcAuthenticatedEncryptorConfiguration |
| 161 | + { |
| 162 | + // Passed to BCryptOpenAlgorithmProvider |
| 163 | + EncryptionAlgorithm = "AES", |
| 164 | + EncryptionAlgorithmProvider = null, |
| 165 | + |
| 166 | + // Specified in bits |
| 167 | + EncryptionAlgorithmKeySize = 256, |
| 168 | + |
| 169 | + // Passed to BCryptOpenAlgorithmProvider |
| 170 | + HashAlgorithm = "SHA256", |
| 171 | + HashAlgorithmProvider = null |
| 172 | + }); |
| 173 | + // </snippet_AddDataProtectionUseCustomCryptographicAlgorithmsCngCbc> |
| 174 | + } |
| 175 | + |
| 176 | + public static void AddDataProtectionUseCustomCryptographicAlgorithmsCngGcm(WebApplicationBuilder builder) |
| 177 | + { |
| 178 | + // <snippet_AddDataProtectionUseCustomCryptographicAlgorithmsCngGcm> |
| 179 | + builder.Services.AddDataProtection() |
| 180 | + .UseCustomCryptographicAlgorithms( |
| 181 | + new CngGcmAuthenticatedEncryptorConfiguration |
| 182 | + { |
| 183 | + // Passed to BCryptOpenAlgorithmProvider |
| 184 | + EncryptionAlgorithm = "AES", |
| 185 | + EncryptionAlgorithmProvider = null, |
| 186 | + |
| 187 | + // Specified in bits |
| 188 | + EncryptionAlgorithmKeySize = 256 |
| 189 | + }); |
| 190 | + // </snippet_AddDataProtectionUseCustomCryptographicAlgorithmsCngGcm> |
| 191 | + } |
| 192 | +#pragma warning restore CA1416 // Validate platform compatibility |
| 193 | +} |
0 commit comments