写此文,是希望有高手能用.Net下与AD相关的类或者名字空间写出类似解决方案。 
需求:本系统不在两个域中的任何一个域中,但是要求验证登陆的帐号(形如 UserName@Dmain)是否在两个域中的其中的一个域中。不能建立信任域。 
小技巧:利用 Net Use命令登陆的返回值,并隐藏 命令提示符的窗口。 

/** <summary> 
         /// 向域控制器发送Net Use 命令 ,返回登陆 正确与否字符可串 
         /// </summary> 
         ///  <param name="ip">域控制器IP</param> 
         /// <param name="Domain">域名</param> 
         /// <param name="strName">登陆帐号</param> 
         /// <param name="strPassWord">登陆密码</param> 
         /// <returns></returns> 
         public string[]  StartApp(string ip,string Domain,string strName,string strPassWord) 
         { 
             Process app=new Process(); 
             string netCMD=" use \\\\"+ip+"\\ipc$ "+strPassWord+" /user:"+Domain+"\\"+strName; 
             ProcessStartInfo info=new ProcessStartInfo(@"net",netCMD); 
             info.RedirectStandardError=true; 
             info.RedirectStandardOutput=true; 
             info.UseShellExecute=false; 
             info.CreateNoWindow = true; 
             app.StartInfo=info; 
             app.Start(); 
             app.WaitForExit(); 
              
             StreamReader reader=app.StandardOutput; 
             string ok=reader.ReadLine(); 
             string no=app.StandardError.ReadLine(); 
             string [] appStr=new string[2]; 
             appStr[0]=ok; 
             appStr[1]=no; 
             this.ExitApp(ip); 
             return appStr; 
         }  /** <summary> 
         /// 关闭Net Use到域控制器上本地的连接   
         /// </summary> 
         /// <param name="ip">域控制器的IP</param> 
         public void ExitApp(string ip) 
         { 
             Process app=new Process(); 
             string netCMD=" use \\\\"+ip+"\\ipc$ /del"; 
             ProcessStartInfo info=new ProcessStartInfo(@"net",netCMD); 
             info.CreateNoWindow = true; 
             app.StartInfo=info; 
             app.Start(); 
             app.WaitForExit(); 
         } /** <summary> 
         /// 检查用户是否是域中合法用户 
         /// </summary> 
         ///  <param name="ip">域控制器IP</param> 
         /// <param name="Domain">域名</param> 
         /// <param name="strName">登陆帐号</param> 
         /// <param name="strPassWord">登陆密码</param> 
         /// <returns>返回是否连接上信息,true 连接上;false,则没有连接上</returns> 
         public bool CheckDomain(string ip,string Domain ,string strName,string strPassWord) 
         { 
             string [] appStr=this.StartApp(ip,Domain,strName,strPassWord); 
             bool CheckOK=false; 
             if(appStr[0]!=null) 
             { 
                 CheckOK=true; 
             } 
             if(appStr[1]!=null) 
             { 
                 CheckOK=false; 
             } 
             return CheckOK; 
         }


登陆按纽事件: 

string Domain1=System.Configuration.ConfigurationSettings.AppSettings["Domain1"].ToString(); 
                         string IP1=System.Configuration.ConfigurationSettings.AppSettings["IP1"].ToString(); 
                         string Domain2=System.Configuration.ConfigurationSettings.AppSettings["Domain2"].ToString(); 
                         string IP2=System.Configuration.ConfigurationSettings.AppSettings["IP2"].ToString(); 
                         if(Domain==Domain1) 
                         { 
                             if(this.CheckDomain(IP1,Domain1,strName,strPassWord)==false) 
                             { 
                                 throw new Exception("用户名或密码出错"); 
                             }

这只是一个简单的用C#写的WEB服务器,只实现了get方式的对html文件的请求,有兴趣的朋友可以在此基础之上继续开发更多功能,小弟学c#不久,如有错漏,望请见凉!! 
摘要:

WWW的工作基于客户机/服务器计算模型,由Web 浏览器(客户机)和Web服务器(服务器)构成,两者之间采用超文本传送协议(HTTP)进行通信,HTTP协议的作用原理包括四个步骤:连接,请求,应答。根据上述HTTP协议的作用原理,本文实现了GET请求的Web服务器程序的方法,通过创建TcpListener类对象,监听端口8080; 等待、接受客户机连接到端口8080; 创建与socket字相关联的输入流和输出流;然后,读取客户机的请求信息,若请求类型是GET,则从请求信息中获取所访问的HTML文件名,如果HTML文件存在,则打开HTML文件,把HTTP头信息和HTML文件内容通过socket传回给Web浏览器,然后关闭文件。否则发送错误信息给Web浏览器。最后,关闭与相应Web浏览器连接的socket字。

一、HTTP协议的作用原理

WWW是以Internet作为传输媒介的一个应用系统,WWW网上最基本的传输单位是Web网页。WWW的工作基于客户机/服务器计算模型,由Web 浏览器(客户机)和Web服务器(服务器)构成,两者之间采用超文本传送协议(HTTP)进行通信。HTTP协议是基于TCP/IP协议之上的协议,是Web浏览器和Web服务器之间的应用层协议,是通用的、无状态的、面向对象的协议。HTTP协议的作用原理包括四个步骤:

连接:Web浏览器与Web服务器建立连接,打开一个称为socket(套接字)的虚拟文件,此文件的建立标志着连接建立成功。

请求:Web浏览器通过socket向Web服务器提交请求。HTTP的请求一般是GET或POST命令(POST用于FORM参数的传递)。GET命令的格式为:

GET 路径/文件名 HTTP/1.0

文件名指出所访问的文件,HTTP/1.0指出Web浏览器使用的HTTP版本。

应答:Web浏览器提交请求后,通过HTTP协议传送给Web服务器。Web服务器接到后,进行事务处理,处理结果又通过HTTP传回给Web浏览器,从而在Web浏览器上显示出所请求的页面。

例:假设客户机与www.mycomputer.com:8080/mydir/index.html建立了连接,就会发送GET命令:GET /mydir/index.html HTTP/1.0。主机名为www.mycomputer.com的Web服务器从它的文档空间中搜索子目录mydir的文件index.html。如果找到该文件,Web服务器把该文件内容传送给相应的Web浏览器。 

为了告知 Web浏览器传送内容的类型,Web服务器首先传送一些HTTP头信息,然后传送具体内容(即HTTP体信息),HTTP头信息和HTTP体信息之间用一个空行分开。

常用的HTTP头信息有:

① HTTP 1.0 200 OK

这是Web服务器应答的第一行,列出服务器正在运行的HTTP版本号和应答代码。代码“200 OK”表示请求完成。

② MIME_Version:1.0

它指示MIME类型的版本。

③ content_type:类型

这个头信息非常重要,它指示HTTP体信息的MIME类型。如:content_type:text/html指示传送的数据是HTML文档。

④ content_length:长度值

它指示HTTP体信息的长度(字节)。

关闭连接:当应答结束后,Web浏览器与Web服务器必须断开,以保证其它Web浏览器能够与Web服务器建立连接。

二、C#实现Web服务器功能的程序设计

根据上述HTTP协议的作用原理,实现GET请求的Web服务器程序的方法如下:

创建TcpListener类对象,监听某端口(任意输入闲置端口 如:8080 )。

等待、接受客户机连接到该端口,得到与客户机连接的socket;

从与socket关联的输入流中读取一行客户机提交的请求信息,请求信息的格式为:GET 路径/文件名 HTTP/1.0

从请求信息中获取请求类型。如果请求类型是GET,则从请求信息中获取所访问的HTML文件名。没有HTML文件名时,则以index.html作为文件名;

如果HTML文件存在,则打开HTML文件,把HTTP头信息和HTML文件内容通过socket传回给Web浏览器,然后关闭文件。否则发送错误信息给Web浏览器;

关闭与相应Web浏览器连接的socket字。

实现的代码如下: 

//webserver.cs// 
  
 namespace cnnbsun.webserver 
 { 
 using System;  
 using System.IO; 
 using ; 
 using .Sockets; 
 using System.Text; 
 using System.Threading ; 
  
  
 class MyWebServer  
 { 
  
 private TcpListener myListener ; 
 private int port = 8080 ; // 选者任何闲置端口 
  
 //开始兼听端口 
 //同时启动一个兼听进程 
 public MyWebServer() 
 { 
 try 
 { 
 //开始兼听端口 
 myListener = new TcpListener(port) ; 
 myListener.Start(); 
 Console.WriteLine("Web Server Running Press ^C to Stop"); 
 //同时启动一个兼听进程 'StartListen' 
 Thread th = new Thread(new ThreadStart(StartListen)); 
 th.Start() ; 
  
 } 
 catch(Exception e) 
 { 
 Console.WriteLine("兼听端口时发生错误 :" +e.ToString()); 
 } 
 } 
 public void SendHeader(string sHttpVersion, string sMIMEHeader, int iTotBytes, string sStatusCode, ref Socket mySocket) 
 { 
  
 String sBuffer = ""; 
  
 if (sMIMEHeader.Length == 0 ) 
 { 
 sMIMEHeader = "text/html"; // 默认 text/html 
 } 
  
 sBuffer = sBuffer + sHttpVersion + sStatusCode + "\r\n"; 
 sBuffer = sBuffer + "Server: cx1193719-b\r\n"; 
 sBuffer = sBuffer + "Content-Type: " + sMIMEHeader + "\r\n"; 
 sBuffer = sBuffer + "Accept-Ranges: bytes\r\n"; 
 sBuffer = sBuffer + "Content-Length: " + iTotBytes + "\r\n\r\n"; 
  
 Byte[] bSendData = Encoding.ASCII.GetBytes(sBuffer);  
  
 SendToBrowser( bSendData, ref mySocket); 
  
 Console.WriteLine("Total Bytes : " + iTotBytes.ToString()); 
  
 } 
  
 public void SendToBrowser(String sData, ref Socket mySocket) 
 { 
 SendToBrowser (Encoding.ASCII.GetBytes(sData), ref mySocket); 
 } 
  
 public void SendToBrowser(Byte[] bSendData, ref Socket mySocket) 
 { 
 int numBytes = 0; 
  
 try 
 { 
 if (mySocket.Connected) 
 { 
 if (( numBytes = mySocket.Send(bSendData, bSendData.Length,0)) == -1) 
 Console.WriteLine("Socket Error cannot Send Packet"); 
 else 
 { 
 Console.WriteLine("No. of bytes send {0}" , numBytes); 
 } 
 } 
 else 
 Console.WriteLine("连接失败."); 
 } 
 catch (Exception e) 
 { 
 Console.WriteLine("发生错误 : {0} ", e ); 
  
 } 
 } 
 public static void Main()  
 { 
 MyWebServer MWS = new MyWebServer(); 
 } 
 public void StartListen() 
 { 
  
 int iStartPos = 0; 
 String sRequest; 
 String sDirName; 
 String sRequestedFile; 
 String sErrorMessage; 
 String sLocalDir; 
 /**//注意设定你自己的虚拟目录/ 
 String sMyWebServerRoot = "E:\\MyWebServerRoot\\"; //设置你的虚拟目录 
 /**/// 
 String sPhysicalFilePath = ""; 
 String sFormattedMessage = ""; 
 String sResponse = ""; 
  
  
 while(true) 
 { 
 //接受新连接 
 Socket mySocket = myListener.AcceptSocket() ; 
  
 Console.WriteLine ("Socket Type " +mySocket.SocketType );  
 if(mySocket.Connected) 
 { 
 Console.WriteLine("\nClient Connected!!\n==================\nCLient IP {0}\n",mySocket.RemoteEndPoint) ; 
  
 Byte[] bReceive = new Byte[1024] ; 
 int i = mySocket.Receive(bReceive,bReceive.Length,0) ; 
  
  
 //转换成字符串类型 
 string sBuffer = Encoding.ASCII.GetString(bReceive); 
  
  
 //只处理"get"请求类型 
 if (sBuffer.Substring(0,3) != "GET" ) 
 { 
 Console.WriteLine("只处理get请求类型.."); 
 mySocket.Close(); 
 return; 
 } 
  
 // 查找 "HTTP" 的位置 
 iStartPos = sBuffer.IndexOf("HTTP",1); 
  
  
 string sHttpVersion = sBuffer.Substring(iStartPos,8); 
  
  
 // 得到请求类型和文件目录文件名 
 sRequest = sBuffer.Substring(0,iStartPos - 1); 
  
 sRequest.Replace("\\","/"); 
  
  
 //如果结尾不是文件名也不是以"/"结尾则加"/" 
 if ((sRequest.IndexOf(".") <1) && (!sRequest.EndsWith("/"))) 
 { 
 sRequest = sRequest + "/";  
 } 
  
  
 //得带请求文件名 
 iStartPos = sRequest.LastIndexOf("/") + 1; 
 sRequestedFile = sRequest.Substring(iStartPos); 
  
  
 //得到请求文件目录 
 sDirName = sRequest.Substring(sRequest.IndexOf("/"), sRequest.LastIndexOf("/")-3); 
  
  
 //获取虚拟目录物理路径 
 sLocalDir = sMyWebServerRoot; 
  
 Console.WriteLine("请求文件目录 : " + sLocalDir); 
  
 if (sLocalDir.Length == 0 ) 
 { 
 sErrorMessage = "<H2>Error!! Requested Directory does not exists</H2><Br>"; 
 SendHeader(sHttpVersion, "", sErrorMessage.Length, " 404 Not Found", ref mySocket); 
 SendToBrowser(sErrorMessage, ref mySocket); 
 mySocket.Close(); 
 continue; 
 } 
  
  
 if (sRequestedFile.Length == 0 ) 
 { 
 // 取得请求文件名 
 sRequestedFile = "index.html"; 
 } 
  
  
 /**// 
 // 取得请求文件类型(设定为text/html) 
 /**// 
  
 String sMimeType = "text/html"; 
  
 sPhysicalFilePath = sLocalDir + sRequestedFile; 
 Console.WriteLine("请求文件: " + sPhysicalFilePath); 
  
  
 if (File.Exists(sPhysicalFilePath) == false) 
 { 
  
 sErrorMessage = "<H2>404 Error! File Does Not Exists</H2>"; 
 SendHeader(sHttpVersion, "", sErrorMessage.Length, " 404 Not Found", ref mySocket); 
 SendToBrowser( sErrorMessage, ref mySocket); 
  
 Console.WriteLine(sFormattedMessage); 
 } 
  
 else 
 { 
 int iTotBytes=0; 
  
 sResponse =""; 
  
 FileStream fs = new FileStream(sPhysicalFilePath, FileMode.Open, FileAccess.Read, FileShare.Read); 
  
 BinaryReader reader = new BinaryReader(fs); 
 byte[] bytes = new byte[fs.Length]; 
 int read; 
 while((read = reader.Read(bytes, 0, bytes.Length)) != 0)  
 { 
 sResponse = sResponse + Encoding.ASCII.GetString(bytes,0,read); 
  
 iTotBytes = iTotBytes + read; 
  
 } 
 reader.Close();  
 fs.Close(); 
  
 SendHeader(sHttpVersion, sMimeType, iTotBytes, " 200 OK", ref mySocket); 
 SendToBrowser(bytes, ref mySocket); 
 //mySocket.Send(bytes, bytes.Length,0); 
  
 } 
 mySocket.Close();  
 } 
 } 
 } 
  
  
 } 
  
 } 
                           } 
                         else if(Domain==Domain2) 
                         { 
                             if(this.CheckDomain(IP2,Domain2,strName,strPassWord)==false) 
                             { 
                                 throw new Exception("用户名或密码出错"); 
                             } 
                         } 
                         else 
                         { 
                             throw new Exception("填写的域不在域中,请检查域名是否写错"); 
                         } 
                     }


希望高手能提供AD类下相关解决方法。