验证域的另一方法

写此文,是希望有高手能用.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("用户名或密码出错");
                            }

                        }

                        
else if(Domain==Domain2)
                        
{
                            
if(this.CheckDomain(IP2,Domain2,strName,strPassWord)==false)
                            
{
                                
throw new Exception("用户名或密码出错");
                            }

                        }

                        
else
                        
{
                            
throw new Exception("填写的域不在域中,请检查域名是否写错");
                        }

                    }

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

posted on 2005-03-10 16:33  CoolBug  阅读(1249)  评论(0编辑  收藏  举报