C# 使用.NET以编程方式生成机器密钥?

C# 使用.NET以编程方式生成机器密钥?,c#,asp.net,.net,vb.net,iis,C#,Asp.net,.net,Vb.net,Iis,我正在使用ASP.NET网站部署实用程序,我需要自定义安装程序能够以与IIS接口相同的方式生成安全的机器密钥。在.NET中使用第一方功能是否可能实现这一点 我正在寻找基于.NET的本机解决方案,而不是powershell或command 行。NET 4+IIS 7+我已经有一段时间没有使用它了,但我认为这应该可以: class Program { static void Main(string[] argv) { int count ; if ((argv.Length ==

我正在使用ASP.NET网站部署实用程序,我需要自定义安装程序能够以与IIS接口相同的方式生成安全的机器密钥。在.NET中使用第一方功能是否可能实现这一点

我正在寻找基于.NET的本机解决方案,而不是powershell或command
行。NET 4+IIS 7+

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

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();
}
}
类程序
{
静态void Main(字符串[]argv)
{
整数计数;
if((argv.Length==0)| |!Int32.TryParse(argv[0],out count))
计数=1;
for(int i=0;i
我已经有一段时间没有使用它了,但我认为这应该是可行的:

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();
}
}
类程序
{
静态void Main(字符串[]argv)
{
整数计数;
if((argv.Length==0)| |!Int32.TryParse(argv[0],out count))
计数=1;
for(int i=0;i