2

我有 2 个应用程序,一个在 MVC 5 中,另一个在 asp.net core 2.2 上

我在 system.web 下的 MVC 5 上添加了机器密钥,如下所示

<machineKey
 validationKey="0C0D6B2776BE432EE3B1554D5C8F88C168944B7E9B7A0FFC885DDE9E9AFA093A"
 decryptionKey="09CCCB69B54D74DB1C2379AB13371EA3D6743227BE6E0092EA3FB762D53668A6"
 validation="SHA1"
 decryption="AES"
 />

我想为 .net 核心应用相同的密钥。我找到了一些使用数据保护的链接,但我未能将这些密钥添加到其中。

有什么方法可以将密钥应用于 asp.net 核心

4

2 回答 2

0

我想为 .net 核心应用相同的密钥。我找到了一些使用数据保护的链接,但我未能将这些密钥添加到其中。

ASP.NET Core 不使用web.config,即使使用了,它也不使用机器密钥。加密是通过Data Protection处理的,它以完全不同的方式存储和生成密钥。

如果您需要在 ASP.NET 和 ASP.NET Core 之间共享诸如cookie 之类的东西,则必须以相反的方式工作并将其插入Data Protection API到您的 ASP.NET 应用程序中,以便它们使用与 ASP.NET Core 相同的系统。

参考:

https://docs.microsoft.com/en-us/aspnet/core/security/cookie-sharing?view=aspnetcore-3.1

于 2020-07-03T04:51:17.973 回答
0

你知道我可以使用的任何数据保护 API。我的主要问题是我正在使用负载平衡和 Redis + SignalR 来共享数据,并且我已经禁用了粘性会话,所以我读到我必须相同的机器密钥,所以我计划在上面实现 Liek。我的 Asp.Net Signalr 应用程序在多个服务器上工作正常,但是当我尝试将消息发送到 asp.net 核心时它不起作用。请提出任何实现他的好方法

您似乎加密了来自 MVC 5 的消息,并希望解密来自部署在另一台机器上的 Asp.Net Core 2.2 应用程序的消息。MachineKey仅在 .NET Framework 下受支持。

要跨应用程序框架加密/解密数据,您需要使用同时支持 .NET Framework 和 .Net Core 的类。您可以从 .NET Standard(例如System.Security.Cryptography)中引用它。下面是使用Aes在 .NET Framework 和 .Net Core 之间跨控制台应用程序的演示:

.Net Framework,控制台应用加密数据

    static void Main(string[] args)
    {
        string original = "Here is some data to encrypt!";

        // Create a new instance of the Aes
        // class.  This generates a new key and initialization
        // vector (IV).
        using (Aes myAes = Aes.Create())
        {
            File.WriteAllBytes("key.data", myAes.Key);
            File.WriteAllBytes("IV.data", myAes.IV);
            // Encrypt the string to an array of bytes.
            byte[] encrypted = EncryptStringToBytes_Aes(original, myAes.Key, myAes.IV);
            File.WriteAllBytes("encrypted.data", encrypted);
            Console.WriteLine("Original:   {0}", original);
        }

        return;
    }

   

    static byte[] EncryptStringToBytes_Aes(string plainText, byte[] Key, byte[] IV)
    {
        // Check arguments.
        if (plainText == null || plainText.Length <= 0)
            throw new ArgumentNullException("plainText");
        if (Key == null || Key.Length <= 0)
            throw new ArgumentNullException("Key");
        if (IV == null || IV.Length <= 0)
            throw new ArgumentNullException("IV");
        byte[] encrypted;

        // Create an Aes object
        // with the specified key and IV.
        using (Aes aesAlg = Aes.Create())
        {
            aesAlg.Key = Key;
            aesAlg.IV = IV;

            // Create an encryptor to perform the stream transform.
            ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

            // Create the streams used for encryption.
            using (MemoryStream msEncrypt = new MemoryStream())
            {
                using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                {
                    using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                    {
                        //Write all data to the stream.
                        swEncrypt.Write(plainText);
                    }
                    encrypted = msEncrypt.ToArray();
                }
            }
        }

        // Return the encrypted bytes from the memory stream.
        return encrypted;
    }

.Net核心控制台应用,解密数据

    static void Main(string[] args)
    {
        // Decrypt the bytes to a string.
        //string roundtrip = DecryptStringFromBytes_Aes(encrypted, myAes.Key, myAes.IV);
   
        var key = File.ReadAllBytes("key.data");
        var iv = File.ReadAllBytes("IV.data");
        var encryptedData= File.ReadAllBytes("encrypted.data");
        // Decrypt the bytes to a string.
        string roundtrip = DecryptStringFromBytes_Aes(encryptedData, key, iv);
 
        Console.WriteLine("Round Trip: {0}", roundtrip);
    }



    static string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key, byte[] IV)
    {
        // Check arguments.
        if (cipherText == null || cipherText.Length <= 0)
            throw new ArgumentNullException("cipherText");
        if (Key == null || Key.Length <= 0)
            throw new ArgumentNullException("Key");
        if (IV == null || IV.Length <= 0)
            throw new ArgumentNullException("IV");

        // Declare the string used to hold
        // the decrypted text.
        string plaintext = null;

        // Create an Aes object
        // with the specified key and IV.
        using (Aes aesAlg = Aes.Create())
        {
            aesAlg.Key = Key;
            aesAlg.IV = IV;

            // Create a decryptor to perform the stream transform.
            ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);

            // Create the streams used for decryption.
            using (MemoryStream msDecrypt = new MemoryStream(cipherText))
            {
                using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                {
                    using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                    {

                        // Read the decrypted bytes from the decrypting stream
                        // and place them in a string.
                        plaintext = srDecrypt.ReadToEnd();
                    }
                }
            }
        }

        return plaintext;
    }

您也可以参考下面的链接了解有关目标框架的详细信息。

SDK 风格项目中的目标框架

于 2020-07-07T03:33:43.643 回答