【问题标题】:Generating Machine Keys programmatically with .NET?使用 .NET 以编程方式生成机器密钥?
【发布时间】:2017-07-30 17:42:57
【问题描述】:

我正在开发一个 ASP.NET 网站部署实用程序,我需要自定义安装程序能够像 IIS 界面一样生成安全机器密钥。这是否可以在 .NET 中使用第一方功能?

我正在寻找基于 .NET 的原生解决方案,而不是 powershell 或命令 线。 .NET 4+ IIS 7+

【问题讨论】:

    标签: c# asp.net .net vb.net iis


    【解决方案1】:

    我已经有一段时间没有使用它了,但我认为这应该可以工作:

    class Program
    {
    static void Main(string[] argv)
    {
        int count ; 
        if ((argv.Length == 0) || !Int32.TryParse(argv[0], out count))
            count = 1;
    
        for (int i = 0; i < count; i++)
        {
            var keyConfig = GetASPNET20machinekey();
            Console.WriteLine(keyConfig);
        }
    }
    
    public static string GetRandomKey(int bytelength)
    {
        byte[] buff = new byte[bytelength];
        RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
        rng.GetBytes(buff);
        StringBuilder sb = new StringBuilder(bytelength * 2);
        for (int i = 0; i < buff.Length; i++)
            sb.Append(string.Format("{0:X2}", buff[i]));
        return sb.ToString();
    }
    
    public static string GetASPNET20machinekey()
    {
        StringBuilder aspnet20machinekey = new StringBuilder();
        string key64byte = GetRandomKey(64);
        string key32byte = GetRandomKey(32);
        aspnet20machinekey.Append("<machineKey \n");
        aspnet20machinekey.Append("validationKey=\"" + key64byte + "\"\n");
        aspnet20machinekey.Append("decryptionKey=\"" + key32byte + "\"\n");
        aspnet20machinekey.Append("validation=\"SHA1\" decryption=\"AES\"\n");
        aspnet20machinekey.Append("/>\n");
        return aspnet20machinekey.ToString();
    }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-10-05
      • 2018-08-20
      • 1970-01-01
      • 1970-01-01
      • 2010-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-28
      相关资源
      最近更新 更多