Configure SSL on a Microsoft.Web.Administration.Ap

2019-05-04 13:51发布

问题:

I'm having a little trouble figuring out how to programatically enable the "Require SSL" check box and change the "Client Certificates" option to "Require" in IIS Application using Microsoft.Web.Administration.

All of the questions on the net I've found relate to configuring it on the web site itself, but I need only need to turn it on for a few Applications hosted on the site.

Anyone have experience doing this?

I tried this to start:

application.EnabledProtocols = "http,https";
serverManager.CommitChanges();

But I got an error:

Filename: \?\C:\Windows\system32\inetsrv\config\applicationHost.config Error: Cannot write configuration file

Which is odd, because from what I've read, I gather that file maintains IIS configuration, but it doesn't exist in Windows (I can't open it in TextPad), but it does exist in the command prompt and I can more it.

回答1:

Did more searching, found this, ended up writing this:

ServerManager serverManager = new ServerManager();
Configuration config = serverManager.GetApplicationHostConfiguration();
ConfigurationSection accessSection = config.GetSection("system.webServer/security/access", siteName + "/" + vdRelativePath);
accessSection["sslFlags"] = "Ssl,SslRequireCert";

Where siteName and vdRelativePath are "Default Web Site" and "/Application/Path".

You can use config.GetLocationPaths() to find the right path if "Default Web Site/Application/Path" doesn't work for you.



标签: c# iis-7 ssl