写此文,是希望有高手能用.Net下与AD相关的类或者名字空间写出类似解决方案。
需求:本系统不在两个域中的任何一个域中,但是要求验证登陆的帐号(形如
UserName@Dmain
)是否在两个域中的其中的一个域中。不能建立信任域。
小技巧:利用 Net Use命令登陆的返回值,并隐藏 命令提示符的窗口。
///
<summary>
/// 向域控制器发送NetUse命令 ,返回登陆 正确与否字符可串
/// </summary>
/// <paramname="ip"> 域控制器IP </param>
/// <paramname="Domain"> 域名 </param>
/// <paramname="strName"> 登陆帐号 </param>
/// <paramname="strPassWord"> 登陆密码 </param>
/// <returns></returns>
public string []StartApp( string ip, string Domain, string strName, string strPassWord)
{
Processapp = new Process();
string netCMD = " use\\\\ " + ip + " \\ipc$ " + strPassWord + " /user: " + Domain + " \\ " + strName;
ProcessStartInfoinfo = new ProcessStartInfo( @" net " ,netCMD);
info.RedirectStandardError = true ;
info.RedirectStandardOutput = true ;
info.UseShellExecute = false ;
info.CreateNoWindow = true ;
app.StartInfo = info;
app.Start();
app.WaitForExit();
StreamReaderreader = 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;
}
/// 向域控制器发送NetUse命令 ,返回登陆 正确与否字符可串
/// </summary>
/// <paramname="ip"> 域控制器IP </param>
/// <paramname="Domain"> 域名 </param>
/// <paramname="strName"> 登陆帐号 </param>
/// <paramname="strPassWord"> 登陆密码 </param>
/// <returns></returns>
public string []StartApp( string ip, string Domain, string strName, string strPassWord)
{
Processapp = new Process();
string netCMD = " use\\\\ " + ip + " \\ipc$ " + strPassWord + " /user: " + Domain + " \\ " + strName;
ProcessStartInfoinfo = new ProcessStartInfo( @" net " ,netCMD);
info.RedirectStandardError = true ;
info.RedirectStandardOutput = true ;
info.UseShellExecute = false ;
info.CreateNoWindow = true ;
app.StartInfo = info;
app.Start();
app.WaitForExit();
StreamReaderreader = 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>
/// 关闭NetUse到域控制器上本地的连接
/// </summary>
/// <paramname="ip"> 域控制器的IP </param>
public void ExitApp( string ip)
{
Processapp = new Process();
string netCMD = " use\\\\ " + ip + " \\ipc$/del " ;
ProcessStartInfoinfo = new ProcessStartInfo( @" net " ,netCMD);
info.CreateNoWindow = true ;
app.StartInfo = info;
app.Start();
app.WaitForExit();
}
/// 关闭NetUse到域控制器上本地的连接
/// </summary>
/// <paramname="ip"> 域控制器的IP </param>
public void ExitApp( string ip)
{
Processapp = new Process();
string netCMD = " use\\\\ " + ip + " \\ipc$/del " ;
ProcessStartInfoinfo = new ProcessStartInfo( @" net " ,netCMD);
info.CreateNoWindow = true ;
app.StartInfo = info;
app.Start();
app.WaitForExit();
}
///
<summary>
/// 检查用户是否是域中合法用户
/// </summary>
/// <paramname="ip"> 域控制器IP </param>
/// <paramname="Domain"> 域名 </param>
/// <paramname="strName"> 登陆帐号 </param>
/// <paramname="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;
}
/// 检查用户是否是域中合法用户
/// </summary>
/// <paramname="ip"> 域控制器IP </param>
/// <paramname="Domain"> 域名 </param>
/// <paramname="strName"> 登陆帐号 </param>
/// <paramname="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( " 填写的域不在域中,请检查域名是否写错 " );
}
}
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类下相关解决方法。