简体   繁体   English

如何使用C#在IIS中启用HTTPS?

[英]How to Enable HTTPS in IIS using C#?

Programmatically (C#) I want to configure IIS site in https. 我要以编程方式(C#)在https中配置IIS站点。 I have created a self-signing certificate and manually configured the site. 我创建了一个自签名证书,并手动配置了该站点。 But I want to automate this process using C#. 但是我想使用C#自动执行此过程。 I can't find a proper solution anywhere. 我在任何地方都找不到合适的解决方案。 Can someone help me with this? 有人可以帮我弄这个吗?

Finally, I got the solution. 最后,我找到了解决方案。 This is the code i wanted. 这是我想要的代码。

ServerManager serverManager = new ServerManager();
Site mySite = serverManager.Sites.Add(siteName.ToString(), "http", "*:80:" + domainName, physicalPath);

X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);                
store.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadWrite);                
X509Certificate2 certificate = new X509Certificate2("SSL server certificate", "password", X509KeyStorageFlags.Exportable);                
store.Add(certificate);      

var binding = mySite.Bindings.Add("*:443:" + domainName, certificate.GetCertHash(), "My");                
binding.Protocol = "https";                
mySite.ApplicationDefaults.ApplicationPoolName = siteName;                
serverManager.CommitChanges();                
store.Close();

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

相关问题 使用 Powershell SDK 和 C# 启用 IIS - Enable IIS using Powershell SDK and C# 如何使用c#在IIS 6和IIS 7中启用32位应用程序模式 - How to enable 32-bit applications mode in IIS 6 and IIS 7 using c# 如何使用WMI / ADSI和C#在IIS中启用/禁用/获取身份验证扩展保护状态? - How to enable/disable/get status for extended protection for authentication in IIS using WMI/ADSI and C#? 如何在没有 IIS 的情况下在 C# Windows 服务中启用远程通信? - How to enable remote communication in a C# Windows Service without IIS? 如何使用C#检测IIS版本? - How to detect IIS version using C#? 如何使用C#启用和禁用蓝牙 - How to enable and disable Bluetooth using C# 如何在IIS上启用读取C#Excel文件(在运行IIS的机器上本地读取它们可以正常工作)? - How to enable reading C# Excel files on IIS (reading them locally on the machine where IIS is running works fine)? 如何在IIS 6.0中正确启用匿名访问并通过c#console app使用MVC Web API? - How to properly enable Anonymous Access in IIS 6.0 & consume an MVC Web API via c# console app? 使用 C# 管理 IIS - Using C# to administer IIS 如何使用iis通过https托管网站 - how to host a site with https using iis
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM