.NET Core - Data Protection in web farm

  

Scenario:

Persist Data Protections to DB store for web farm

Solution:

1. Implement custom IXmlRepository

     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
    42
    43
    44
     public class DBKeyRepository : IXmlRepository
        {
            private readonly IServiceScopeFactory _factory;
    
            public DBKeyRepository(IServiceScopeFactory factory)
            {
                _factory = factory;
            }
    
            public IReadOnlyCollection<XElement> GetAllElements()
            {
                using (var scope = _factory.CreateScope())
                {
                    return GetKeys();
                }
            }
    
            public void StoreElement(XElement element, string friendlyName)
            {
                var key = new XmlKey
                {
                    Xml = element.ToString(SaveOptions.DisableFormatting)
                };
    
                using (var scope = _factory.CreateScope())
                {
                    PersistKeys(key);
                }
            }
    
            private List<XElement> GetKeys()
            {
                var result = new List<XElement>();
    
                //get Keys to DB //XElement.Parse(xml)
    
                return result;
            }
    
            private void PersistKeys(XmlKey key)
            {
                //persist Keys to DB
            }
        }
2. XmlKey.cs

    1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    public class XmlKey
        {
            public Guid Id { get; set; }
            public string Xml { get; set; }
    
            public XmlKey()
            {
                this.Id = Guid.NewGuid();
            }
        }


3.Startup.cs
     
    1
    2
    3
    4
    5
    6
    7
    8
    9
     public void ConfigureServices(IServiceCollection services)
            {
             ....
                services.AddOptions<KeyManagementOptions>()
                    .Configure<IServiceScopeFactory>((options, factory) =>
                    {
                        options.XmlRepository = new DBKeyRepository(factory);
                    });
             ...
            }

No comments:

Post a Comment

Move Github Sub Repository back to main repo

 -- delete .gitmodules git rm --cached MyProject/Core git commit -m 'Remove myproject_core submodule' rm -rf MyProject/Core git remo...