Project: Enumerate.Bindings.For.Application


Run LINQPad as administrator.

Enumerate bindings (domains associated) for a web application in IIS using the Microsoft.Web.Administration library (http://msdn.microsoft.com/en-us/library/microsoft.web.administration.aspx)
void Main()
{
	if (!IsRunningAsAdmin())
	{
		"To manage IIS run LINQPad as administrator.".Dump();
		return;
	}
	
	var serverManager = new ServerManager();
	
	var sites = 
		new[] { new { Index = 0, Name = string.Empty } }
		.AsEnumerable();
	
	var	sites =
		serverManager
			.Sites
			.Select((site, index) => new { Index = ++index, Name = site.Name })
			.OrderBy(site => site.Name)
			.Dump();
	
	Console.WriteLine("Select the website to get bindings information:");
	
	var websiteIndex = int.Parse(Console.ReadLine());
	var siteName = sites.Single(x => x.Index == websiteIndex).Name;
	
	serverManager
		.Sites
		.Single(s => s.Name == siteName)
		.Bindings.Select(x => x.BindingInformation).Dump();
}

private bool IsRunningAsAdmin()
{
	var isAdmin = false;
	var currentIdentity = WindowsIdentity.GetCurrent();
	
	if (currentIdentity != null)
	{
		WindowsPrincipal pricipal = new WindowsPrincipal(currentIdentity);
		isAdmin = pricipal.IsInRole(WindowsBuiltInRole.Administrator);
		pricipal = null;
	}
	
	return isAdmin;
}

Last edited Apr 8, 2012 at 9:03 PM by teoarch, version 3

Comments

No comments yet.