C
C#4d ago
d1ag0n

IDataProtectionBuilder.AddKeyManagementOptions

I have multiple API servers and I want them to be capable of sharing cookies so I have this code. How can I do this better?
c#
WebApplication app = null;
...
builder.Services.AddDataProtection()
.SetApplicationName("MyApplication")
.AddKeyManagementOptions(options => {
var db = ActivatorUtilities.CreateInstance<MyDatabaseService>(app.Services);
options.XmlRepository = new MyXMLRepo(db);
});
...
app = builder.Build();
...
c#
WebApplication app = null;
...
builder.Services.AddDataProtection()
.SetApplicationName("MyApplication")
.AddKeyManagementOptions(options => {
var db = ActivatorUtilities.CreateInstance<MyDatabaseService>(app.Services);
options.XmlRepository = new MyXMLRepo(db);
});
...
app = builder.Build();
...
7 Replies
d1ag0n
d1ag0nOP4d ago
I changed from using the activator to this. Which is okay because only one instance is created now. But I still don't like that I'm getting a reference to the app. Is this some design fault on my part or it seems I'm stuck behind whatever DataProtection is doing. I guess I could make my own cookies to avoid this.
c#
var db = app.Services.GetRequiredService<MyDatabaseService>();
c#
var db = app.Services.GetRequiredService<MyDatabaseService>();
Kaos
Kaos4d ago
There are a few PersistKeysTo* options that are built in, maybe the dbcontext one would work here? https://learn.microsoft.com/en-us/aspnet/core/security/data-protection/configuration/overview?view=aspnetcore-9.0#persistkeystodbcontext
Configure ASP.NET Core Data Protection
Learn how to configure Data Protection in ASP.NET Core.
d1ag0n
d1ag0nOP4d ago
I'm not using EF.
Kaos
Kaos4d ago
You can look at the pattern that they use and see if that is what you want to do. The azure one's source is a bit easier to get to
d1ag0n
d1ag0nOP4d ago
Yeah I need to ask about redis lpush and lrange because I basically copied the pattern from https://github.com/dotnet/aspnetcore/blob/main/src/DataProtection/StackExchangeRedis/src/RedisDataProtectionBuilderExtensions.cs https://github.com/StackExchange/StackExchange.Redis/blob/main/src/StackExchange.Redis/RedisDatabase.cs#L1334 I'm not sure what LPUSH does. Apparently it puts stuff on the left? And LRANGE seems to go from left to right? I think it's ultimately LIFO. But I'm not sure.
Kaos
Kaos4d ago
That seems to be the case based on the regis docs https://redis.io/docs/latest/commands/lpush/
LPUSH
Prepends one or more elements to a list. Creates the key if it doesn't exist.
d1ag0n
d1ag0nOP4d ago
Okay yeah I read that lastnight and I guess I was tired.

Did you find this page helpful?