C#怎么實(shí)現(xiàn)AD+NT+Normal認(rèn)證

本篇內(nèi)容介紹了“C#怎么實(shí)現(xiàn)AD+NT+Normal認(rèn)證”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

創(chuàng)新互聯(lián)專注于宣漢網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供宣漢營(yíng)銷型網(wǎng)站建設(shè),宣漢網(wǎng)站制作、宣漢網(wǎng)頁(yè)設(shè)計(jì)、宣漢網(wǎng)站官網(wǎng)定制、微信小程序服務(wù),打造宣漢網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供宣漢網(wǎng)站排名全網(wǎng)營(yíng)銷落地服務(wù)。

代碼如下:

鑒于此,要使用Forms認(rèn)證方式

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沒(méi)有做退出動(dòng)作
        if (Request.QueryString["Flag"] != "Y")

        {

            //初始化,抓取NT帳號(hào)
            if (Request.ServerVariables["LOGON_USER"] != "")

            {

                FormsAuthentication.RedirectFromLoginPage(Request.ServerVariables["LOGON_USER"], true);

            } 

        }

        //否則,進(jìn)行Normal or AD驗(yàn)證
        else

        {

         

        }

    } 
    protected void btnLogin_Click(object sender, EventArgs e)

    {

            //判斷非AD賬號(hào)
            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驗(yàn)證
            if (objData.CheckNTAccount(txtName.Text, txtPwd.Text, drpDomain.SelectedValue)) 

            {

                FormsAuthentication.RedirectFromLoginPage(txtName.Text, true);

            }

            //其他非AD賬號(hào)
            else if(ds.Tables[0].Rows.Count!=0)    

            {

                FormsAuthentication.RedirectFromLoginPage(txtName.Text, true);

            }

            //如果不能成功登錄,顯示出錯(cuò)信息 
            else 

            {

                Response.Write("<script type=text/javascript>alert(\'請(qǐng)輸入正確的密碼,賬號(hào)~\');</script>");

            }

        

    }
4.default.aspx.cs
    protected void lbtnLogout_Click(object sender, EventArgs e)

    {

        string strFlag = "flag=Y";

        FormsAuthentication.RedirectToLoginPage(strFlag);

    }
5.clsData.cs(AD驗(yàn)證)
using System.DirectoryServices;   //此為AD驗(yàn)證的控件,一定要加載?。。?
/// <summary>
/// clsData 的摘要說(shuō)明
/// </summary>
public class clsData

{

    public clsData()

    {

        //

        // TODO: 在此處添加構(gòu)造函數(shù)邏輯

        //
    }

    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

“C#怎么實(shí)現(xiàn)AD+NT+Normal認(rèn)證”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

網(wǎng)站標(biāo)題:C#怎么實(shí)現(xiàn)AD+NT+Normal認(rèn)證
網(wǎng)頁(yè)網(wǎng)址:http://muchs.cn/article36/picdsg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、虛擬主機(jī)、網(wǎng)站內(nèi)鏈、小程序開(kāi)發(fā)、外貿(mào)建站、網(wǎng)站營(yíng)銷

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化