简体   繁体   English

以编程方式生成IIS应用程序密钥

[英]Generate IIS application key programmatically

Using the IIS manager GUI, it is easy to use the 'Connections' tree view to navigate to an application, under ASP.Net open 'Machine Key', and then click 'Generate Keys' in order to generate a key for your application for the purpose of setting up a shared ASP.Net session across machines. 使用IIS管理器GUI,很容易在ASP.Net下使用“连接”树视图导航到应用程序,打开“机器密钥”,然后单击“生成密钥”以为您的应用程序生成密钥。建立跨机器的共享ASP.Net会话的目的。

However, when I read the documentation for Generate a Machine Key , the only thing I get is the cryptic 但是,当我阅读“ 生成机器密钥”的文档时,唯一得到的是神秘的

Use the following WMI classes, methods, or properties to perform this procedure: 使用以下WMI类,方法或属性来执行此过程:

MachineKeySection class. MachineKeySection类。 Keys are generated by setting properties, so no method is needed.) 密钥是通过设置属性生成的,因此不需要任何方法。)

The incorrect punctuation does not boost my confidence and I'm not sure how to implement this advice regardless. 错误的标点符号并不能增强我的信心,而且我不确定如何实施此建议。 How do I generate validation and decryption keys for my IIS application via the command line? 如何通过命令行为IIS应用程序生成验证和解密密钥? I'm also willing to generate them via C#. 我也愿意通过C#生成它们。

You can use ManagementClass class from System.Management to connect to WMI. 您可以使用System.Management中的ManagementClass类连接到WMI。 Using this class you can manipulate WMI instances. 使用此类,您可以操纵WMI实例。 Please follow below mentioned steps: 请按照以下提到的步骤操作:

  1. Create new console application. 创建新的控制台应用程序。

  2. Add reference to System.Management assembly. 添加对System.Management程序集的引用。

  3. Add using statement as follows 如下添加using语句

     using System.Management 
  4. Use below code to connect to MachineKeySection class. 使用以下代码连接到MachineKeySection类。

     string NamespacePath = "\\\\\\\\.\\\\ROOT\\\\WebAdministration"; string ClassName = "MachineKeySection"; ManagementClass oClass = new ManagementClass(NamespacePath + ":" + ClassName); foreach (ManagementObject oObject in oClass.GetInstances()) { Console.WriteLine("ApplicationName : {0}", oObject["ApplicationName"]); } 
  5. You can use following screenshot to check available properties and methods. 您可以使用以下屏幕截图检查可用的属性和方法。 WMI Explorer WMI资源管理器

在此处输入图片说明

I guess you could use the RNGCryptoServiceProvider class to generate the required keys using PowerShell. 我猜您可以使用RNGCryptoServiceProvider类使用PowerShell生成所需的密钥。 It is the same class IIS manager itself uses to generate those keys. IIS管理器本身使用该类来生成这些密钥。

I have learned from this blog about the way IIS manager is using that class. 我从这个博客中学到了IIS管理器使用该类的方式。

With IIS 7 you no longer have to do this manually. 使用IIS 7,您不再需要手动执行此操作。 The IIS 7.0 manager has a built in feature that you can use to generate these keys. IIS 7.0管理器具有内置功能,可用于生成这些密钥。 It uses RNGCryptoServiceProvider internally to create a random key. 它在内部使用RNGCryptoServiceProvider创建一个随机密钥。

The post author is a Microsoft employee - I do not know him personally though. 帖子作者是Microsoft员工-虽然我个人不认识他。

Here is one example of using it with the PowerShell script approach. 是将其与PowerShell脚本方法一起使用的一个示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 以编程方式将Web应用程序导入IIS - Import a web application to IIS programmatically IIS7以编程方式修改应用程序匿名访问 - IIS7 programmatically modify application anonymous access 以编程方式停止应用程序池问题IIS7 - STOP Application Pool Issue Programmatically IIS7 以编程方式更改IIS 7应用程序物理路径 - Change IIS 7 application physical path programmatically 使用安装程序以编程方式在IIS中创建应用程序池时出错 - Error creating an Application Pool in IIS programmatically with an installer IIS通过命令行为Jenkins生成机器密钥 - IIS generate machine key by command line for Jenkins 以编程方式将子应用程序添加到现有IIS应用程序 - Adding a sub-application to an existing IIS application programmatically 有没有办法以编程方式检查每个Web应用程序上一次在IIS中运行的时间? - Is there a way to programmatically check the last time each web application was run in IIS? 在IIS 7中以编程方式为自定义身份应用程序池设置用户帐户 - Programmatically set up user account for custom identity application pool in IIS 7 以编程方式将IIS应用程序池标识“用户”分配给组 - Programmatically assigning IIS Application Pool Identity “users” to Groups
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM