dotnet / dotnet/AspNetCore.Docs
Valid DataProtection Storage customization. Given documentation is wrong.
- Dominant language
- C#
- Stars
- 13.1k
- Forks
- 24.6k
- Avg merge
- 1d 3h
- Merged PRs (30d)
- 97
Description
The given documentation is wrong and confusing since it ignores the concept of IoC.
If one wants to customize Data Protection keys, we do not want to 'new' objects thus requiring to supply construction for classes in a duplicate way.
Here is a sample, and I think the only way to do it, here, e.g. using MemCached as a storage provider.
```
public class MemCachedDataProtector: IXmlRepository
{
private readonly IMemcachedClient _memcachedClient;
private readonly ILogger _logger;
public MemCachedDataProtector( IMemcachedClient client, ILoggerFactory loggerFactory)
{
_memcachedClient = client;
_logger = loggerFactory.CreateLogger();
}
public IReadOnlyCollection GetAllElements()
{
var exists = _memcachedClient.TryGet("dp", out Dictionary lst);
if (!exists)
{
_logger.LogInformation("No protection keys found");
return Array.Empty();
}
_logger.LogInformation($"found {lst.Count} protection keys");
return lst.Values.Select(XElement.Parse).ToArray();
}
public void StoreElement(XElement element, string friendlyName)
{
var xml = element.ToString(SaveOptions.DisableFormatting);
var exists = _memcachedClient.TryGet("dp", out Dictionary lst);
if (!exists)
{
lst = new Dictionary();
}
if (lst.ContainsKey(friendlyName))
{
_logger.LogInformation($"Adding ({friendlyName}) as Data Protection key");
lst[friendlyName] = xml;
}
else
{
_logger.LogInformation($"Updating ({friendlyName}) as Data Protection key");
lst.Add(friendlyName, xml);
}
_memcachedClient.Set("dp", lst, (int)TimeSpan.FromDays(30*3).TotalSeconds);
}
}
public static class MemCachedExtension
{
///
/// Persists Data Protection Keys to MemCached
/// using
///
///
public static IDataProtectionBuilder PersistKeysToMemCached(this IDataProtectionBuilder builder)
{
ArgumentNullException.ThrowIfNull(builder, nameof(builder));
builder.Services.AddSingleton();
builder.Services.AddSingleton((Func>)delegate (IServiceProvider sp)
{
var memCachedDataProtected = sp.GetRequiredService();
return new ConfigureOptions(options =>
{
options.XmlRepository = memCachedDataProtected;
});
});
return builder;
}
}
```
---
#### Document Details
⚠ *Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.*
* ID: f34a00d6-1197-1a89-6fb6-9adcc4643850
* Version Independent ID: 46f5eaf3-930a-7442-ad4f-4615c38d03b8
* Content: [Key management extensibility in ASP.NET Core](https://learn.microsoft.com/en-us/aspnet/core/security/data-protection/extensibility/key-management?view=aspnetcore-7.0)
* Content Source: [aspnetcore/security/data-protection/extensibility/key-management.md](https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/security/data-protection/extensibility/key-management.md)
* Product: **aspnet-core**
* Technology: **aspnetcore-security**
* GitHub Login: @Rick-Anderson
* Microsoft Alias: **riande**
Contributor guide
Assessment
This issue has not been assessed yet.