asp.net 使用AD进行身份进行验证

//在登录界面增加一个AD验证方法

    private static DirectoryEntry GetDirectoryObject(string userName,string userPwd)
    {

        DirectoryEntry entry = null;
        try
        {

            entry = new DirectoryEntry("LDAP://192.168.10.5:389/DC=ecera,DC=com,DC=tw", userName, userPwd, AuthenticationTypes.Secure);
        }

        catch (Exception ex)
        {
            throw ex;
        }

        return entry;
    } 

//在登录按钮下如此使用

        DirectoryEntry entry = GetDirectoryObject(this.txtUserName.Value,this.txtUserPwd.Value);
        DirectorySearcher search = new DirectorySearcher(entry);
        search.Filter = "(SAMAccountName=" + this.txtUserName.Value+ ")";
        try
        {

            SearchResult result = search.FindOne();

            if (result != null)
            {

                string userID = this.txtUserName.Value;
                Session.Add("UserName", userID);
                Session.Add("UserTrueName", UserInfoBll.Instance.GetUserName(userID));
                Response.Redirect("~/SystemPage.htm");

            }

        }

        catch (Exception e1)
        {

            labMsg.Text =e1.Message.ToString() ;

        }  
一切尽在淘婚网www.taohunla.com
原文地址:https://www.cnblogs.com/litao9026/p/2992902.html