ASP和C#.NET实现IIS6添加和删除站点主机头

 【代码】
               
 1  using  System;
 2  using  System.DirectoryServices;
 3 
 4  namespace  PPT.Biz
 5  {
 6       ///   <summary>
 7       ///  HostHead 的摘要说明。
 8       ///   </summary>
 9       public   class  HostHead
10      {
11           public  HostHead()
12          {
13              
14          }
15          
16           private   static   int  siteId  = 752236766 ; // 站点的ID
17          
18           ///   <summary>
19           ///  DirectoryEntry site = new DirectoryEntry("IIS://localhost/W3SVC/1/root");
20           ///  说明:
21           ///  siteid,默认站点是1,对于非默认站点,通过查看站点日志文件名就可以知道。
22           /// 或者直接查看文件C:/WINDOWS/system32/inetsrv/MetaBase.xml
23           /// 如果提示没有访问权限,请尝试使用虚拟帐号:
24           /// <identity impersonate="true" userName="managerisdavid.weihw" password="@#$%&amp;%#$%sdfgsdaf34756" />
25           ///   </summary>
26           ///   <param name="siteid"></param>
27           ///   <param name="ip"></param>
28           ///   <param name="port"></param>
29           ///   <param name="domain"></param>
30           public   static   void  AddHostHeader( int  siteid,  string  ip,  int  port,  string  domain) // 增加主机头(站点编号.ip.端口.域名)
31          {
32              DirectoryEntry site  =   new  DirectoryEntry( " IIS://localhost/W3SVC/ "   +  siteid);
33              PropertyValueCollection serverBindings  =  site.Properties[ " ServerBindings " ];
34               string  headerStr  =   string .Format( " {0}:{1}:{2} " , ip, port, domain);
35               if  ( ! serverBindings.Contains(headerStr))
36              {
37                  serverBindings.Add(headerStr);
38              }
39              site.CommitChanges();
40          }
41 
42           public   static   void  DeleteHostHeader( int  siteid,  string  ip,  int  port,  string  domain) // 增加主机头(站点编号.ip.端口.域名)
43          {
44              DirectoryEntry site  =   new  DirectoryEntry( " IIS://localhost/W3SVC/ "   +  siteid);
45              PropertyValueCollection serverBindings  =  site.Properties[ " ServerBindings " ];
46               string  headerStr  =   string .Format( " {0}:{1}:{2} " , ip, port, domain);
47               if  (serverBindings.Contains(headerStr))
48              {
49                  serverBindings.Remove (headerStr);
50              }
51              site.CommitChanges();
52          }
53 
54 
55           public   static   void  AddHostHeader( string  domain)
56          {
57              DirectoryEntry site  =   new  DirectoryEntry( " IIS://localhost/W3SVC/ " + siteId);
58              
59              PropertyValueCollection serverBindings  =  site.Properties[ " ServerBindings " ];
60               string  headerStr  =   string .Format( " {0}:{1}:{2} " , null , 80 ,domain);
61               if  ( ! serverBindings.Contains(headerStr))
62              {
63                  serverBindings.Add(headerStr);
64              }
65              site.CommitChanges();
66          }
67 
68           public   static   void  DeleteHostHeader( string  domain)
69          {
70              DirectoryEntry site  =   new  DirectoryEntry( " IIS://localhost/W3SVC/ " + siteId);
71          
72              PropertyValueCollection serverBindings  =  site.Properties[ " ServerBindings " ];
73               string  headerStr  =   string .Format( " {0}:{1}:{2} " , null , 80 ,domain);
74               if  (serverBindings.Contains(headerStr))
75              {
76                  serverBindings.Remove(headerStr);
77              }
78              site.CommitChanges();
79          }
80      }
81  }

【说明】
            1、添加主机头时,可能提示无访问权限之类的错误,一般可以采用如下的方法解决:

              A、当允许匿名登录时,IIS将一个匿名登录使用的标识(缺省情况下是IUSR_MACHINENAME)交给ASP.NET应用程序。可以尝试修改该标识的访问权限。
         
             B、模拟指定的用户帐号。
            在Web.config文件的<identity>标记中指定具体的用户帐号:
            <identity impersonate="true" userName="accountname" password="password" />这时该ASP.NET应用程序的所有页面的所有请求都将以指定的用户帐号权限执行。

请参考:微软中国社区

ASP实现站点主机头的添加与删除

< script >
function  check()
{
   var yourname
= document.form1.yourname.value
   
if (yourname == "" ||fucCheckyourname(yourname) == 0 )
   {
      alert(
" 请输入域名或者您申请的域名不合法,请重新选择 " );
   return 
false ;
   document.form1.yourname.focus;
   }
   
   
else
   
   return 
true ;
}

function  fucCheckyourname(yourname)
{
 var i,j,strTemp;
 strTemp
= " 0123456789abcdefghijklmnopqrstuvwxyz " ;
 
for  (i = 0 ;i < yourname.length;i ++  )
 {
  j
= strTemp.indexOf(yourname.charAt(i)); 
  
if  (j ==- 1 )
  {
   
// 说明有字符不合法
   return 
0 ;
  }
 }
 
// 说明合法
 return 
1 ;
}
</ script >

< form id = " form1 "  name = " form1 "  method = " post "  action = "" >
  
< label > 请选择域名:Http: //
< input type = " text "  name = " yourname " />
  
</ label >  
  .xawgd.cn
  
< label >
  
< input type = " submit "  name = " Submit "  value = " 提交 "  onclick = " return check() "   />
  
< input type = " hidden "  name = " flag "  value = " add "   />
  
</ label >
</ form >
<
 
if  request.Form( " flag " ) = " add "   then
  dnsname
= request.Form( " yourname " )
  dnsname
= dnsname & i
  AddWebSiteHostName 
1 , " :7655: " & "" & dnsname & "" & " .xawgd.cn "      ' 访问ip,端口,主机头名"
  response.Write( " <script>alert('申请成功,你申请的站点域名为http:// " & dnsname & " .xawgd.cn')</script> " )
  Response.Write(
" <script>window.opener=null;window.close();</script> " )  
 
end   if
%
>
<
' ===============================================================
'
函数名称:AddWebSiteHostName
'
函数功能:在已存在的站点中主机头中添加新的主机头
'
           index 站点编号
'
           NewWebTop 主机头字符串
'
例如:NewWebTop="192.168.0.163:80:aaa.xawgd.cn"'访问ip,端口,主机头名
'
     index="1" 站点编号。1为默认站点
'
===============================================================
Public   Function  AddWebSiteHostName(index, NewWebTop)

  
On   Error   Resume   Next
  
  
Dim  tmpwebtop, ztop, oWebServer, tmpstring, Computer
 
  Computer 
=   " localhost "
  
  
Set  oWebServer  =   Getobject ( " IIS:// "   &  Computer  &   " /w3svc/ "   &  index)
  
  tmpwebtop 
=  oWebServer.serverbindings  ' 获取原来的主机头
  
  tmpstring 
=   Join (tmpwebtop,  " ||| " )
  
  
If   InStr (tmpstring,  " ||| "   &  NewWebTop  &   " ||| " Or   InStr (tmpstring,  " ||| "   &  NewWebTop)  Or   InStr (tmpstring, NewWebTop  &   " ||| " =   1   Then
     
     oWebServer.serverbindings 
=  tmpwebtop
     
  
Else
     
     
ReDim  Preserve tmpwebtop( UBound (tmpwebtop)  +   1 )
     
     tmpwebtop(
UBound (tmpwebtop))  =  NewWebTop
   
     oWebServer.serverbindings 
=  tmpwebtop
     
  
End   If

  tmpwebtop 
=  oWebServer.serverbindings
  tmpstring 
=   Join (tmpwebtop,  " ||| " )
  
  oWebServer.SetInfo

End Function
%
>

你可能感兴趣的:(JOIN,null,asp.net,domain,asp,IIS)