Base solution for your next web application
Open Closed

Data Protection Mechanism #7953


User avatar
0
evbenerji created

Dear support,

I've reviewed what you wrote before about data protection; https://support.aspnetzero.com/QA/Questions/247

Could you please give me more information about how to integrate Data Protection mechanism into Asp.net zero?

Regards.


1 Answer(s)
  • User Avatar
    1
    musa.demir created

    As mentioned, there is no built-in mechanism for that. I dont know your need but one of the way is using custom mapping. Create custom mapper.

    [DependsOn(typeof(AbpAutoMapperModule))]
    public class MyModule : AbpModule
    {
        public override void PreInitialize()
        {
            Configuration.Modules.AbpAutoMapper().Configurators.Add(config =>
                    {
                        config.CreateMap<EntityDto, Entity>()
                                          .ForMember(u => u.ProtectedData, options => options => options.MapFrom(input => MyCipher.Encrypt(input.ProtectedData)));
    
                        config.CreateMap<Entity, EntityDto>()
                                          .ForMember(u => u.ProtectedData, options => options => options.MapFrom(input => MyCipher.Decrypt(input.ProtectedData)));                  
                    });
               }
    }
    

    When you get data from db and need to use protected data map it to dto.

    var dto = (IObjectMapper).Map<EntityDto>(entity);
    //dto.ProtectedData is decrypted now
    

    When you want to store data to db map to entity.

    var entity = (IObjectMapper).Map<Entity>(dto);
    //entity.ProtectedData is encrypted now
    

    You can implement something like that. Check https://aspnetboilerplate.com/Pages/Documents/Object-To-Object-Mapping#custom-mapping for more information about mapping.