Forked from georgiosd/DataProtectionBuilderExtensions.cs
Created
April 18, 2025 14:13
-
-
Save mdissel/4443b8098982cca82720350c92e151ab to your computer and use it in GitHub Desktop.
RavenDB ASP.NET Data Protection Repository
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Collections.ObjectModel; | |
using System.Globalization; | |
using System.Linq; | |
using System.Xml.Linq; | |
using Microsoft.AspNetCore.DataProtection.KeyManagement; | |
using Microsoft.AspNetCore.DataProtection.Repositories; | |
using Microsoft.Extensions.DependencyInjection; | |
using Microsoft.Extensions.Options; | |
using Raven.Client.Documents; | |
using Raven.Client.Documents.Session; | |
namespace Microsoft.AspNetCore.DataProtection | |
{ | |
public static class DataProtectionBuilderExtensions | |
{ | |
public static IDataProtectionBuilder PersistKeysToRaven(this IDataProtectionBuilder builder) | |
{ | |
if (builder == null) | |
{ | |
throw new ArgumentNullException(nameof(builder)); | |
} | |
builder.Services.AddSingleton<IConfigureOptions<KeyManagementOptions>>(services => | |
{ | |
var store = services.GetRequiredService<IDocumentStore>(); | |
return new ConfigureOptions<KeyManagementOptions>(options => | |
{ | |
options.XmlRepository = new RavenXmlRepository(store); | |
}); | |
}); | |
return builder; | |
} | |
class RavenXmlRepository : IXmlRepository | |
{ | |
private readonly IDocumentStore store; | |
public RavenXmlRepository(IDocumentStore store) | |
{ | |
this.store = store; | |
} | |
public IReadOnlyCollection<XElement> GetAllElements() | |
{ | |
using var session = store.OpenSession(); | |
var docs = session.Advanced | |
.LoadStartingWith<XmlDocument>(CollectionName + "/", | |
pageSize: int.MaxValue); | |
return new ReadOnlyCollection<XElement>( | |
docs.Select(x => XElement.Parse(x.SerializedXml)).ToArray()); | |
} | |
public void StoreElement(XElement element, string friendlyName) | |
{ | |
string? id = element.Attribute("id")?.Value; | |
if (id == null | |
|| !DateTime.TryParse(element.Element("expirationDate")?.Value, null, DateTimeStyles.AssumeUniversal, out var expirationDate)) | |
{ | |
throw new NotSupportedException($"Not supported Xml: {element}"); | |
} | |
using var session = store.OpenSession(); | |
var document = new XmlDocument | |
{ | |
Id = $"{CollectionName}/{id}", | |
SerializedXml = element.ToString() | |
}; | |
session.Store(document); | |
var metadata = session.Advanced.GetMetadataFor(document); | |
meta[Constants.Documents.Metadata.Expires] = expirationDate; | |
session.SaveChanges(); | |
} | |
private const string CollectionName = "XmlDocuments"; | |
class XmlDocument | |
{ | |
public string Id { get; set; } = null!; | |
public string SerializedXml { get; set; } = null!; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment