Viewing Active Directory Objects via ASP.NET http://www.wimdows.net/articles/printme.aspx?aid=13http://www.wimdows.net/articles/downloads/ADSI.zip

解决方案 »

  1.   

    http://justdn.org/justdn.v2/content.php?ID=46
      

  2.   

    (1).读取计算机的名称: 
    在名称空间System.Net中定义了一个类Dns,在此类中定义了一个比较重要的方法 GetHostName ( ),此方法的返回值就是本地计算机名称。在程序设计中首先要导入System.Net名称空间,然后通过调用Dns类中的GetHostName ( )方法读取本地计算机名称,具体实现的主要语句如下: 
    label1.Text = "主机名称:" + System.Net.Dns.GetHostName ( ) ; 
    (2).读取计算机的拨号上网临时的IP地址和局域网分配的固定IP地址: 
    在程序设计中,我们是通过一个自定义的函数--getIPAddress ( )来读取IP地址的。首先看一下如何读取本地固定的IP地址的。在Dns类中还定义了一个方法GetHostByName ( )。此方法的返回值时IPHostEntry 对象,此对象有一个属性是AddressList,此属性是一个IPAddress类型的数组,包含了计算机此时的所有的IP地址信息。这当中也就包含了拨号上网得到的临时分配的IP地址和局域网固定的IP地址。具体实现语句如下: 
    private static string getIPAddress ( ) 

    System.Net.IPAddress addr; 
    // 获得本机局域网IP地址 
    addr = new System.Net.IPAddress ( Dns.GetHostByName ( Dns.GetHostName ( ) ) .AddressList [0].Address ) ; 
    return addr.ToString ( ) ; 
    } 四.读取计算机名称本机固定IP地址源程序 
    IP01.cs源程序: 
    //导入程序用到的名称空间 
    using System ; 
    using System.Net; 
    using System.Windows.Forms ; 
    using System.Drawing ; 
    public class Form3 : Form 

    //定义二个标签 
    private Label label1 ; 
    private Label label2 ; 
    public static void Main ( ) 

    Application.Run ( new Form3 ( ) ) ; 

    // 构造窗体 
    public Form3 ( ) 

    // 建立标签并且初始化 
    this.label1 = new System.Windows.Forms.Label ( ) ; 
    this.label2 = new System.Windows.Forms.Label ( ) ; 
    //先继承一个Label 类 
    label1.Location = new System.Drawing.Point ( 24 , 16 ) ; 
    label2.Location = new System.Drawing.Point ( 44 , 36 ) ; 
    //设定 Label的显示位置 
    label1.Text = "主机名称:" + System.Net.Dns.GetHostName ( ) ; 
    // 显示本机的计算机名称 
    label2.Text = "IP 地址:" + getIPAddress ( ) ; 
    // 显示本机的局域网IP地址 
    label1.Size = new System.Drawing.Size ( 200 , 50 ) ; 
    label2.Size = new System.Drawing.Size ( 200 , 80 ) ; 
    //设定标签的大小 
    label1.TabIndex = 0 ; 
    label2.TabIndex = 1 ; 
    label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ; 
    label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ; 
    // 设定标签的对齐方式 
    this.Text = "获得主机名称和IP地址!" ; 
    this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent ; 
    this.AutoScaleBaseSize = new System.Drawing.Size ( 8 , 16 ) ; 
    this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D ; 
    // 设定窗体的边界类型 
    this.ForeColor = System.Drawing.SystemColors.Desktop ; 
    this.Font = new System.Drawing.Font ( "宋体" , 10 , System.Drawing.FontStyle.Bold ) ; 
    // 设定字体、大小就字体的式样 
    this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide ; 
    this.ClientSize = new System.Drawing.Size ( 250 , 250 ) ; 
    //把标签加到窗体中 
    this.Controls.Add ( this.label1 ) ; 
    this.Controls.Add ( this.label2 ) ; 

    private static string getIPAddress ( ) 

    System.Net.IPAddress addr; 
    // 获得本机局域网IP地址 
    addr = new System.Net.IPAddress ( Dns.GetHostByName ( Dns.GetHostName ( ) ) .AddressList [0].Address ) ; 
    return addr.ToString ( ) ; 

      

  3.   

    <%@ Page Language="C#" %>
    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Collections"%><%@ Import Namespace="System.Net" %><%
    NameValueCollection myheader;
    int i;
    string cookiesstr;
    myheader=Request.Headers;
    int totalnum=myheader.Count;
    try
    {
      for(i=0;i<totalnum;i++)
        { cookiesstr=myheader.GetKey(i);
      Response.Write(" Name " + myheader.GetKey(i) + " ---- Value : " + myheader.Get(cookiesstr) + "<br>");
    }
    }catch(Exception e)
    {
      Response.Write("<错误>: " + e.ToString());
    }
    %>
    <script language="C#" runat="server">
    void Page_load(Object s ,EventArgs e)
    {
    Dns dns;
    string hostname;
    hostname = Dns.GetHostName();
    //IPAddress[] ip = Dns.Resolve(hostname);IPHostEntry hostinfo = Dns.Resolve(hostname);
    IPAddress[] ip = hostinfo.AddressList;Response.Write(" The machine name : " + hostname + "<br> IP Address :" + ip[0].ToString() +"<hr size=1>"  );
    }void doclick(Object s ,EventArgs e)
    {
     try
      { IPHostEntry hostinfo = Dns.GetHostByName( domain.Text );
         showip.Text= "<hr size=1>域名:" +domain.Text + " 的IP地址是:" + hostinfo.AddressList[0].ToString();
      }
      catch(Exception e1)
      {
        showip.Text=" <hr size=1>[错误] : " + e1.ToString();
      }}void doclick2(Object s ,EventArgs e)
    {
     try
      { IPHostEntry hostinfo = Dns.GetHostByAddress( domain2.Text );
         showip2.Text= "<hr size=1>IP地址是:" +domain2.Text + " 的域名是:" + hostinfo.HostName.ToString();
      }
      catch(Exception e1)
      {
        showip2.Text=" <hr size=1>[错误] ]: " + e1.ToString();
      }}
    </script>
    <asp:label id=showip runat="server"/>
    <asp:label id="showip2" runat="server"/>
    <form id="form1" runat="server">
    请输入要查找的域名:
    <asp:textbox id="domain" runat="server" />
    <asp:button id="do" onclick="doclick" runat="server" text="转化为IP地址" />
    <br>
    请输入要查找的IP:
    <asp:textbox id="domain2" runat="server" />
    <asp:button id="do2" onclick="doclick2" runat="server" text="转化为域名" />
    </form>