-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyFileToBase64ConfigurationProvider.cs
More file actions
41 lines (35 loc) · 1.23 KB
/
Copy pathKeyFileToBase64ConfigurationProvider.cs
File metadata and controls
41 lines (35 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Extensions.Configuration;
namespace YourCompany.Configuration.KeyFileToBase64
{
internal sealed class KeyFileToBase64ConfigurationProvider : FileConfigurationProvider
{
private readonly string _basePath;
internal KeyFileToBase64ConfigurationProvider(KeyFileToBase64ConfigurationSource source) : base(source)
{
_basePath = source.BasePath?.TrimEnd(Path.DirectorySeparatorChar) ?? "";
}
public override void Load(Stream stream)
{
byte[] bytes;
using (var memoryStream = new MemoryStream())
{
stream.CopyTo(memoryStream);
bytes = memoryStream.ToArray();
}
Data = new Dictionary<string, string>
{
[GenerateConfigurationKey()] = Convert.ToBase64String(bytes)
};
}
private string GenerateConfigurationKey()
{
var relativePath = Path.GetRelativePath(_basePath, Source.Path);
return relativePath
.Replace(Path.DirectorySeparatorChar, ':')
.Replace(Path.AltDirectorySeparatorChar, ':');
}
}
}