How to implement machineKey in ASP.NET Core 2.0

后端 未结 4 563
傲寒
傲寒 2020-12-03 16:50

In ASP.NET (not core) I would normally add a machineKey to the web.config so that I could perform some functions on a local machine instead of the server so that database/ca

4条回答
  •  死守一世寂寞
    2020-12-03 17:42

    You need to use DataProtection APis now:

    The ASP.NET Core data protection stack provide a simple, easy to use cryptographic API a developer can use to protect data, including key management and rotation.

    Samples could be found in official DataProtection repo.

    The same approach, by the way, works with ASP.NET: Replacing in ASP.NET


    The data protection system is built upon two core concepts - a data protection provider (represented by the IDataProtectionProvider interface), which is used to create a data protector (represented by the IDataProtector interface) by CreateProtector method. The data protector is used to encrypt and decrypt data.

    To register IDataProtectionProvider into DI use .AddDataProtection method:

    public void ConfigureServices(IServiceCollection services)
    {
        // Adds data protection services
        services.AddDataProtection();
        ...
    }
    

提交回复
热议问题