Solved

Dinamically adding multiple identities (hostnames) to a IIS WebSite

Posted on 2008-06-13
4
980 Views
Last Modified: 2013-12-17
Hi, below is a part of a code to create a new IIS website on C#.
The code works perfectly.
I need to put multiple identities on the "ServerBindings" property.
I want that the website will be available on 2 hostnames (test.com and www.test.com).

I tried to do:
site.Invoke("Put", "ServerBindings", string.Format(":80:{0},:80:www.{0}", website_name));
(to put the website_name and www.website_name port 80)
like the way that I see on the MetaBase Explorer (IIS resource kit) but it doesn't work.
 
DirectoryEntry site = (DirectoryEntry)root.Invoke("Create", "IIsWebServer", random_site_id);
 
site.Invoke("Put", "ServerComment", website_name);
 
site.Invoke("Put", "KeyType", "IIsWebServer");
 
site.Invoke("Put", "ServerBindings", string.Format(":80:{0}", website_name));

Open in new window

0
Comment
Question by:giligatech
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 37

Expert Comment

by:meverest
ID: 21808774
Hi,

can't speak much about c#, but from what I can tell you are trying to treat the serverbindigs property like a plain string(?)

This property is actually an array of strings, so you will need to first retrieve the existing array, then append the new value and then set that new array back to the property.

Cheers,  Mike.
0
 

Author Comment

by:giligatech
ID: 21810929
any document i could see and use?
0
 
LVL 37

Accepted Solution

by:
meverest earned 500 total points
ID: 21811296
I can offer these bits of code.  The first script simply lists the web site properties as an xml formatted page.  Call it like this:

http://webserver/path/displayweb.asp?server=localhost&siteid=1

(where server is the name of the web server, and siteid is the site ID from the metabase)

Use this script as input to write data into your html form - they are output as a comma delimited string like:

ipaddress1:port1:hostheader1,ipaddress2:port2:hostheader2,ipaddress3:port3:hostheader3,...

the second script takes the serverbindings array from a form field named "" and writes it to the metabase.  The url is the same format as above (e.g. http://webserver/path/updatehostheaders.asp?server=localhost&siteid=1)

hope that helps a bit.

Cheers.
=== script 1: displayweb.asp ============================================
<%@ Language=VBScript %><%
on error resume next
Dim objW3SVC, objSITE, strOUT
Set objW3SVC = GetObject("IIS://" & CStr(Request.QueryString("server")) & "/W3SVC")
If (Err <> 0) Then FatalErr
query = Request.QueryString("siteid")
'Check Global Properties.
%><%=Request.QueryString("siteid")%>
<%	'Walk through the metabase, and pick out web servers only.
	For Each objSITE in objW3SVC
	response.Write(".")
	If objSITE.class = "IIsWebServer" Then 
    If objSITE.name = query Then
%><xml>
<website>
<description><%= objSITE.ServerComment %></description>
<homedir><%
	    set ServerRoot = GetObject("IIS://" & CStr(Request("server")) & "/w3svc/" & objSITE.name & "/ROOT")
	    If (Err <> 0) Then handleErr 
	    Response.Write serverRoot.Path 
        If (Err <> 0) Then handleErr 
      %></homedir>
<dirsize><%
      Set fso = CreateObject("Scripting.FileSystemObject") 
      Set fold = fso.GetFolder(serverRoot.Path) 
      FormatNumberDemo = FormatNumber(fold.size, 2)
      If FormatNumberDemo < 999 then
      FormatNumberDemo = (Int(FormatNumberDemo * 100))/100 
      Response.Write FormatNumberDemo & " Bytes"
      ElseIf FormatNumberDemo < 999999 then
      FormatNumberDemo = (FormatNumberDemo/1000)
      FormatNumberDemo = (Int(FormatNumberDemo * 100))/100 
      Response.Write FormatNumberDemo & " Kilobytes"
      ElseIf FormatNumberDemo < 999999999 then
      FormatNumberDemo = (FormatNumberDemo/1000000)
      FormatNumberDemo = (Int(FormatNumberDemo * 100))/100 
      Response.Write FormatNumberDemo & " Megabytes"
      ElseIf FormatNumberDemo < 999999999999 then
      FormatNumberDemo = (FormatNumberDemo/1000000000)
      FormatNumberDemo = (Int(FormatNumberDemo * 100))/100 
      Response.Write FormatNumberDemo & " Gigabytes"
      End If
      set fold = nothing 
      set fso = nothing 
      %></dirsize>
<adsipath><% = UCase("lm/w3svc/" & objSITE.Name) %></adsipath>
<logpath><% response.Write objSITE.LogFileDirectory & "\w3svc" & objSITE.Name
            If (Err <> 0) Then handleErr %></logpath>
<ntlmauth><% = IIf ((ServerRoot.AuthFlags and 4) = 4, "Yes", "No") %></ntlmauth>
<basicauth><% = IIf ((ServerRoot.AuthFlags and 2) = 2, "Yes", "No") %></basicauth>
<anonymoususer><% = objSITE.AnonymousUserName %></anonymoususer>
<anonymousaccess><% = IIf ((ServerRoot.AuthFlags and 1) = 1,"Yes","No") %></anonymousaccess>
<status><%  If (Err <> 0) Then handleErr
	if objSITE.ServerState = 1 then 
		Response.write "Starting"
	elseif objSITE.ServerState = 2 then
	      Response.write "Started"
	elseif objSITE.ServerState = 3 then
		Response.write "Stopping"
	elseif objSITE.ServerState = 4 then
		Response.write "Stopped"
	elseif objSITE.ServerState = 5 Then
		Response.write "Pausing"
	elseif objSITE.ServerState = 6 then
		Response.write "Paused"
	elseif objSITE.ServerState = 7 then
		Response.write "Continuing"
	else 
		Response.Write "unknown"
	end if
      %></status>
<defaultdocs><% = serverRoot.DefaultDoc %></defaultdocs>
<hostheaders><%
				For each port in objSITE.ServerBindings
				Response.Write port & ", "
				Next	
			%></hostheaders>
<dirstruct>Root,<%
	         set VirtualDirectory = GetObject("IIS://" & CStr(Request("server")) & "/w3svc/" & objSITE.name & "/ROOT")
	         For Each Item In VirtualDirectory
	         Response.Write Item.Name & ","
             Next
            %></dirstruct>
<maxupload><% = serverRoot.AspMaxRequestEntityAllowed %></maxupload>
 
<%
	End If
	End If
	Next
Set objW3SVC = nothing
Set objSITE = nothing
Set strOUT = nothing
%></website>
</xml>
 
=== script 2: updatehostheaders.asp ============================================
<%@ Language=VBScript %>
<%
on error resume next
siteid = Request.QueryString("siteid")
servername = Request.QueryString("server")
 
NewSB = Request.form("HostHeaders")
IPAddr = Request.form("ServerIPAddress")
 
dim arrStr
dim theStr
 
arrStr = Split(NewSB,",")
 
Dim aryBindings()
ReDim aryBindings( Ubound(arrStr) )
for i=0 to Ubound(arrStr)
	aryBindings(i)= IPAddr & ":80:" & arrStr(i)
next
 
Set objComp = GetObject("IIS://" & servername & "/W3SVC/" & siteid)
	objComp.ServerBindings = aryBindings
	objComp.SetInfo
If (Err <> 0) Then handleErr 
 
Set objComp = nothing
 
Sub FatalErr
	strOUT	= "Fatal Error " & Hex(Err) &	" ( "
	strOUT	= strOUT & Err.Description & " ) occurred."
	Response.Write strOUT
End Sub
%>

Open in new window

0
 

Author Closing Comment

by:giligatech
ID: 31466890
nice...
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

What is an ISAPI filter?   •      It's an assembly (.dll file) that can add or change the way IIS works.   •      They can be enabled globally for your web server or on a site-by-site basis.   When the IIS server receives a request, enabling the ISAPI fi…
Running classic asp applications under Windows Server 2008 R2 (x64) and IIS 7 is not as easy as one may think. It took me a while to figure it out while getting error 8002801d a few times. After you install the OS you will need to install the fol…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question