由于我们在服务结构上,您已经正确部署了一个证书,您的集群可以使用它。否则,您必须添加证书。
var cert = X509.LocalMachine.My.Thumbprint.Find("C03BB5A6410741CDD2927B4FF88C3E67215A393B", validOnly: false).FirstOrDefault();
services.AddApplicationStorageDataProtection(_container, cert);
和
public static IServiceCollection AddApplicationStorageDataProtection(this IServiceCollection services, IUnityContainer container, X509Certificate2 cert )
{
if (container != null)
{
try
{
var storage = container.Resolve<IApplicationStorageService>();
var token = storage.GetApplicationStorageSharedAccessSignature().GetAwaiter().GetResult();
var name = storage.GetApplicationStorageAccountNameAsync().GetAwaiter().GetResult();
var a = new CloudStorageAccount(new StorageCredentials(token), name, null, true);
var c = a.CreateCloudBlobClient().GetContainerReference("dataprotection");
c.CreateIfNotExists();
services.AddDataProtection()
.ProtectKeysWithCertificate(cert)
.PersistKeysToAzureBlobStorage(c.GetBlockBlobReference("dummy.csrf"));
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
throw;
}
}
return services;
}
当然,您必须根据需要更改代码。这里提供的代码使用集群中部署的存储服务,因此我不需要管理所有应用程序中存储的凭据,只需向存储服务请求凭据,而且unity容器就在那里,因为这是我正在使用的底层DI框架。
services.AddDataProtection()
.ProtectKeysWithCertificate(cert)
.PersistKeysToAzureBlobStorage(c.GetBlockBlobReference("dummy.csrf"));