使用IIS管理器,我对mij网站进行了一些绑定。现在我想列出所有绑定在网站上的绑定,我该怎么做呢?
我认为这篇文章应该会有帮助,但这并没有给出域名(只有一些IP地址):ASP.NET core get URL bindings
发布于 2020-07-22 21:33:09
根据您的描述,我建议您可以尝试使用Microsoft.Web.Administration包来帮助您实现您的要求。
我们可以根据您的IIS asp.net核心网站的站点名称获取绑定信息。
由于我们无法在asp.net核心中直接获取IIS站点名称,因此我们将使用try来获取IIS应用程序池标识。
更多细节,你可以参考下面的代码:
API控制器:
[HttpPost("GetIISBinding")]
public ActionResult GetIISBinding()
{
var re = GetBindings();
return Ok(re);
}
private IEnumerable<KeyValuePair<string, string>> GetBindings( )
{
// Get the Site name
//string siteName = _IConfiguration["ASPNETCORE_APPL_PATH"];
string name = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
string siteName = name.Split(@"\")[1];
// Get the sites section from the AppPool.config
Microsoft.Web.Administration.ConfigurationSection sitesSection =
Microsoft.Web.Administration.WebConfigurationManager.GetSection(null, null, "system.applicationHost/sites");
foreach (Microsoft.Web.Administration.ConfigurationElement site in sitesSection.GetCollection())
{
// Find the right Site
if (String.Equals((string)site["name"], siteName, StringComparison.OrdinalIgnoreCase))
{
// For each binding see if they are http based and return the port and protocol
foreach (Microsoft.Web.Administration.ConfigurationElement binding in site.GetCollection("bindings"))
{
string protocol = (string)binding["protocol"];
string bindingInfo = (string)binding["bindingInformation"];
if (protocol.StartsWith("http", StringComparison.OrdinalIgnoreCase))
{
string[] parts = bindingInfo.Split(':');
if (parts.Length == 3)
{
string domian = parts[2];
string port = parts[1];
yield return new KeyValuePair<string, string>(protocol, domian + ":" + port);
}
}
}
}
}
}结果:

更新:
添加应用程序池标识权限。
注意:这可能会使您的服务器不安全。正常情况下,我们不建议您直接添加此权限。
1.打开applicationhost.config文件夹:C:\Windows\System32\inetsrv\config\
2.右键单击applicationhost.config文件并选择属性。
3.单击安全性
5.单击“编辑”,然后单击“添加”按钮单击“位置”按钮,并确保选择本地计算机。(如果服务器属于某个域,则不是Windows域。)
6.在"Enter the object name to select:“文本框中输入"IIS AppPool\DefaultAppPool”。(不要忘记将此处的"DefaultAppPool“更改为您为应用程序池命名的任何名称。)
7.单击“检查名称”按钮,然后单击“确定”。
8.设置该应用程序池的足够权限。
9.重新启动站点,然后重试。
https://stackoverflow.com/questions/63011726
复制相似问题