【Vegas原创】经典的AD+NT+Normal认证(C#版)

鉴于此,要使用Forms认证方式
1。web.config:

     < authentication   mode ="Forms"   >
      
< forms  name ="PlatForm"  defaultUrl ="default.aspx"  path ="/" ></ forms >
    
</ authentication >

 

     < appSettings >
         
    
< add  key ="LDAP"  value ="LDAP://server:389/OU=*,DC=*,DC=*" />
    
</ appSettings >


2。login.aspx:

     < form  id ="form1"  runat ="server" >
    
< div >
     
< p >    < asp:TextBox  ID ="txtName"  runat ="server" ></ asp:TextBox ></ p >  
        
< asp:TextBox  ID ="txtPwd"  runat ="server"  TextMode ="Password" ></ asp:TextBox >
        
< p >< asp:DropDownList  ID ="drpDomain"  runat ="server" >
            
< asp:ListItem  Selected ="True" > aa </ asp:ListItem >
            
< asp:ListItem ></ asp:ListItem >
            
< asp:ListItem  Value ="bb" > bb </ asp:ListItem >
        
</ asp:DropDownList ></ p >   
        
< asp:Button  ID ="btnLogin"  runat ="server"  Text ="登录"  OnClick ="btnLogin_Click"   />
      
</ div >
    
</ form >


3.login.aspx.cs

 DBAccess db  =   new  DBAccess();
    
protected   void  Page_Load( object  sender, EventArgs e)
    {
        
// 如果default.aspx没有做退出动作
         if  (Request.QueryString[ " Flag " !=   " Y " )
        {
            
// 初始化,抓取NT帐号
             if  (Request.ServerVariables[ " LOGON_USER " !=   "" )
            {
                FormsAuthentication.RedirectFromLoginPage(Request.ServerVariables[
" LOGON_USER " ],  true );
            } 
        }
        
// 否则,进行Normal or AD验证
         else
        {
         
        }
    }

 

     protected   void  btnLogin_Click( object  sender, EventArgs e)
    {
            
// 判断非AD账号
             string  strSql = " select * from users where job=1 and account_ID=' " + txtName.Text.Trim() + " ' and psWrd=' " + txtPwd.Text.Trim() + " ' " ;
            DataSet ds
= db.GetDataSet(strSql);

            clsData objData 
=   new  clsData();
            
// AD验证
             if  (objData.CheckNTAccount(txtName.Text, txtPwd.Text, drpDomain.SelectedValue)) 
            {
                FormsAuthentication.RedirectFromLoginPage(txtName.Text, 
true );
            }
            
// 其他非AD账号
             else   if (ds.Tables[ 0 ].Rows.Count != 0 )    
            {
                FormsAuthentication.RedirectFromLoginPage(txtName.Text, 
true );
            }
            
// 如果不能成功登录,显示出错信息 
             else  
            {
                Response.Write(
" <script type=text/javascript>alert(\'请输入正确的密码,账号~\');</script> " );
            }
        
    }


4.default.aspx.cs

     protected   void  lbtnLogout_Click( object  sender, EventArgs e)
    {
        
string  strFlag  =   " flag=Y " ;
        FormsAuthentication.RedirectToLoginPage(strFlag);
    }


5.clsData.cs(AD验证)

using  System.DirectoryServices;    // 此为AD验证的控件,一定要加载!!!
///   <summary>
///  clsData 的摘要说明
///   </summary>
public   class  clsData
{
    
public  clsData()
    {
        
//
        
//  TODO: 在此处添加构造函数逻辑
        
//
    }
    
public   bool  CheckNTAccount( string  UserName,  string  Password,  string  Domain)
    {

        DirectoryEntry Ad;
        
//             DirectorySearcher ds;
         string  strLDAP  =  System.Configuration.ConfigurationSettings.AppSettings[ " LDAP " ];

        Ad 
=   new  DirectoryEntry(strLDAP, Domain  +   " \\ "   +  UserName, Password, AuthenticationTypes.ServerBind);

        
try
        {
            
object  obj  =  Ad.NativeObject;  // 检查是否可正常登入
         
            
return   true ;
        }

        
catch  (Exception)
        {
            
return   false ;
        }

        
finally
        {
            Ad.Dispose();
        }

    }
}


6,DBAccess.cs
    详见:
http://www.cnblogs.com/ReadAuthen.aspx?EntryID=922281&ReturnUrl=/vegaslee/archive/2007/10/12/922281.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值