你好,每个人都回复我的问题?我使用Asp.net在LDAP中收到错误... [英] hello every one reply my question ? iam getting error in LDAP using Asp.net...

查看:62
本文介绍了你好,每个人都回复我的问题?我使用Asp.net在LDAP中收到错误...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 使用系统; 
使用 System.Collections;
使用 System.Configuration;
使用 System.Data;
使用 System.Linq;
使用 System.Web;
使用 System.Web.Security;
使用 System.Web.UI;
使用 System.Web.UI.HtmlControls;
使用 System.Web.UI.WebControls;
使用 System.Web.UI.WebControls.WebParts;
使用 System.Xml.Linq;
使用 System.Text;
使用 System.DirectoryServices;

命名空间 WebApplication4
{
public partial class WebForm1:System.Web.UI.Page
{
protected void Page_Load( object sender,EventArgs e)
{

}

受保护 void btnLogin_Click( object sender,EventArgs e)
{
string dominName = .Empty;
字符串 adPath = 字符串 .Empty;
string userName = txtLoginID.Text.Trim()。ToUpper();
string strError = string .Empty;
尝试
{
foreach string key in ConfigurationSettings.AppSettings.Keys)
{
dominName = key.Contains( DirectoryDomain)? ConfigurationSettings.AppSettings [key]:dominName;
adPath = key.Contains( DirectoryPath)? ConfigurationSettings.AppSettings [key]:adPath;
if (!String.IsNullOrEmpty(dominName)&&!String.IsNullOrEmpty(adPath))
{
if true == AuthenticateUser(dominName,userName,txtPassword.Text,adPath, out strError))
{
Response.Redirect( default .aspx); // 经过身份验证的用户重定向到default.aspx
}
dominName = string .Empty;
adPath = string .Empty;
if String .IsNullOrEmpty(strError))断裂;
}

}
if (!string.IsNullOrEmpty(strError))
{
lblError.Text = 用户名或密码无效!;
}
}
catch
{

}
最后
{

}
}

public bool AuthenticateUser( string domain, string username, string password, string LdapPath, out string Errmsg)
{
Errmsg = ;
string domainAndUsername = domain + @ \\ \\ +用户名;
DirectoryEntry entry = new DirectoryEntry(LdapPath,domainAndUsername,password);
尝试
{
// 绑定到本机AdsObject以强制进行身份验证。
对象 obj = entry.NativeObject;
DirectorySearcher search = new DirectorySearcher(条目);
search.Filter = (SAMAccountName = + username + ;
search.PropertiesToLoad.Add( cn);
SearchResult result = search.FindOne();
if null == result)
{
return false ;
}
// 更新目录中用户的新路径
LdapPath = result.Path;
string _filterAttribute =( String )result.Properties [ cn] [ 0 ];
}
catch (例外情况)
{
Errmsg = ex.Message;
return false ;
throw new 异常( 验证用户时出错。 + ex.Message);
}
返回 true ;
}

protected void btnCancel_Click( object sender,EventArgs e)
{
txtLoginID.Text = string .Empty;
txtPassword.Text = string .Empty;
}
}
}





源代码





 <%@     Page    语言  =  C#    AutoEventWireup   =  true    CodeBehind   =  Default.aspx。 cs   继承  =  WebApplication4._Default   %>  

< !DOCTYPE html PUBLIC - // W3C // DTD XHTML 1.0 Transitional // EN http ://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd >
< html xmlns = http://www.w3.org/1999 / xhtml >
< head id = Head1 runat = 服务器 >
< title > LDAP身份验证< span class =code-keyword>< / title >
< / head >
< body &g t;
< 表格 id = form1 runat = server >
< < span class =code-leadattribute> div >
< div >
< cellpadding = 1 cellspacing = 1 style = background-color:#E0E0E0; border:1px solid#727272 >
< tr >
< td >
< < span class =code-leadattribute> asp:Label ID = lblName runat = server 文本 = 名称 < span class =code-keyword>> < ; / asp:标签 >
< / td >
< td >
< asp:TextBox ID = txtLoginID 宽度 = 150 runat = server > < / asp:TextBox >
< / td >
< / tr >
< tr >
< td >
< asp:标签 ID = lblpwd runat = server 文本 = 密码 > < / asp:Label >
< < span class =code-leadattribute> / td >
< td >
< asp:TextBox ID = txtPass单词 宽度 = 150 TextMode = 密码 runat = 服务器 > ; < / asp:TextBox > ;
< / td >
< / tr >
< tr >
< < span class =code-leadattribute> td colspan = 2 >
< asp:按钮 ID = btnLogin runat = server 文字 = 登录 OnClick = btnLogin_Click / >
& nbsp; < asp:按钮 ID = btnCancel runat = server 文本 = 取消 OnClick = btnCanc el_Click / >
< / td >
< / tr > ;
< tr >
< td colspan = 2 >
< asp:标签 ID = lblError < span class =code-attribute> runat = server ForeColor = 红色 文字 = > < / asp:标签 >
< / td >
< / tr >
< / table >
< / div >
< / div >
< /表格 >
< / body >
< / html >





< br $>


错误是



错误 12 类型命名空间名称'SearchResult'可以找到 (您是否缺少using指令程序集引用?)c:\ usersrs \ srikanth \documents\visual studio 2012 \Projects \WebApplication4 \WebApplication4 \Default.aspx。 cs   76   17  WebApplication4 
错误 1 类型名称空间名称'DirectoryServices'在名称空间'System'中存在 (是你错过了一个程序集引用?)c:\users \ srikanth \documents\visual studio 2012 \Projects\WebApplication4 \WebApplication4 \Default.aspx。 cs 14 14 WebApplication4
错误 10 类型名称空间名称'DirectorySearcher'可以 (您是否缺少using指令程序集引用?)c:\ usersrs \ srikanth \documents\visual studio 2012 \Projects \WebApplication4 \\ \\ WebApplication4 \Default.aspx。 cs 73 17 WebApplication4
错误 11 类型名称空间名称'DirectorySearcher'可以 (您是否缺少using指令汇编引用?)c:\ usersrs \ srikanth \\ \\documents\visual studio 2012 \Projects \WebApplication4 \WebApplication4 \Default.aspx。 cs 7 3 48 WebApplication4
错误 8 类型命名空间名称'DirectoryEntry'可以找到 (您是否缺少using指令程序集引用?)c:\users \srikanth \documents\visual studio 2012 \Projects \WebApplication4 \WebApplication4 \Default.aspx。 cs 68 13 WebApplication4
错误 9 类型名称空间名称'DirectoryEntry'可以找到 (你错过了吗?使用指令程序集引用?)c:\users \srikanth \documents\visual studio 2012 \Projects\WebApplication4 \ WebAppl ication4\Default.aspx。 cs 68 40 WebApplication4
错误 3 名称'txtPassword'在当前上下文中存在 :\users\srikanth \documents\visual studio 2012 \Projects\WebApplication4 \WebApplication4 \Default.aspx。 cs 39 75 WebApplication4
错误 14 名称'txtPassword '存在于当前上下文中c:\users \ srikanth \documents\visual studio 2012 \Projects\WebApplication4 \WebApplication4 \Default .aspx。 cs 97 13 WebApplication4
错误 2 名称'txtLoginID'在当前上下文中存在 c:\ usersrs \ srikanth \ document \ visual studio 2012 \Projects \WebApplication4 \WebApplication4 \Default.aspx。 cs 29 31 WebApplication4
错误 13 名称txtLoginID执行 not 存在于当前上下文中c:\users \ srikanth \\\ documents \ visual studio 2012 \Projects \WebApplication4 \ WebApplication4 \Default.aspx。 cs 96 13 WebApplication4
错误 4 名称'lblError'在当前上下文中存在 c:\ usersrs \ srikanth \\\ documents \ visual studio 2012 \Projects\WebApplication4 \WebApplication4 \Default.aspx。 cs 51 21 WebApplication4
警告 5 'System.Configuration.ConfigurationSettings.AppSettings'已过时:' 此方法已过时,已被System.Configuration取代!System.Configuration.ConfigurationManager.AppSettings'c:\\ \\ users\srikanth \documents\visual studio 2012 \Projects\WebApplication4 \WebApplication4 \Default.aspx。 cs 33 40 WebApplication4
警告 6 'System.Configuration.ConfigurationSettings .AppSettings'已经过时了:' 这个方法已经过时了,它已经过时了en替换为System.Configuration!System.Configuration.ConfigurationManager.AppSettings'c:\ usersrs \ srikanth \documents\visual studio 2012 \Projects\WebApplication4 \ WebApplication4 \Default.aspx 。 cs 35 67 WebApplication4
警告 7 'System.Configuration.ConfigurationSettings.AppSettings'已过时:' 此方法已过时,已被System.Configuration取代!System.Configuration.ConfigurationManager.AppSettings'c:\ usersrs \ srikanth \documents \ visual studio 2012 \ Projects \WebApplication4 \WebApplication4 \Default.aspx。 cs 36 62 WebApplication4

解决方案


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.DirectoryServices;

namespace WebApplication4
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            string dominName = string.Empty;
            string adPath = string.Empty;
            string userName = txtLoginID.Text.Trim().ToUpper();
            string strError = string.Empty;
            try
            {
                foreach (string key in ConfigurationSettings.AppSettings.Keys)
                {
                    dominName = key.Contains("DirectoryDomain") ? ConfigurationSettings.AppSettings[key] : dominName;
                    adPath = key.Contains("DirectoryPath") ? ConfigurationSettings.AppSettings[key] : adPath;
                    if (!String.IsNullOrEmpty(dominName) && !String.IsNullOrEmpty(adPath))
                    {
                        if (true == AuthenticateUser(dominName, userName, txtPassword.Text, adPath, out strError))
                        {
                            Response.Redirect("default.aspx");// Authenticated user redirects to default.aspx
                        }
                        dominName = string.Empty;
                        adPath = string.Empty;
                        if (String.IsNullOrEmpty(strError)) break;
                    }

                }
                if (!string.IsNullOrEmpty(strError))
                {
                    lblError.Text = "Invalid user name or Password!";
                }
            }
            catch
            {

            }
            finally
            {

            }
        }

        public bool AuthenticateUser(string domain, string username, string password, string LdapPath, out string Errmsg)
        {
            Errmsg = "";
            string domainAndUsername = domain + @"\" + username;
            DirectoryEntry entry = new DirectoryEntry(LdapPath, domainAndUsername, password);
            try
            {
                // Bind to the native AdsObject to force authentication.
                Object obj = entry.NativeObject;
                DirectorySearcher search = new DirectorySearcher(entry);
                search.Filter = "(SAMAccountName=" + username + ")";
                search.PropertiesToLoad.Add("cn");
                SearchResult result = search.FindOne();
                if (null == result)
                {
                    return false;
                }
                // Update the new path to the user in the directory
                LdapPath = result.Path;
                string _filterAttribute = (String)result.Properties["cn"][0];
            }
            catch (Exception ex)
            {
                Errmsg = ex.Message;
                return false;
                throw new Exception("Error authenticating user." + ex.Message);
            }
            return true;
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            txtLoginID.Text = string.Empty;
            txtPassword.Text = string.Empty;
        }
    }
}



source code


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication4._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>LDAP Authentication</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <div>
            <table cellpadding="1" cellspacing="1" style="background-color: #E0E0E0; border: 1px solid #727272">
                <tr>
                    <td>
                        <asp:Label ID="lblName" runat="server" Text="Name"></asp:Label>
                    </td>
                    <td>
                        <asp:TextBox ID="txtLoginID" Width="150" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="lblpwd" runat="server" Text="Password"></asp:Label>
                    </td>
                    <td>
                        <asp:TextBox ID="txtPassword" Width="150" TextMode="Password" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <asp:Button ID="btnLogin" runat="server" Text="Login" OnClick="btnLogin_Click" />
                        &nbsp;<asp:Button ID="btnCancel" runat="server" Text="Cancel" OnClick="btnCancel_Click" />
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <asp:Label ID="lblError" runat="server" ForeColor="Red" Text=""></asp:Label>
                    </td>
                </tr>
            </table>
        </div>
    </div>
    </form>
</body>
</html>





error is

Error   12  The type or namespace name 'SearchResult' could not be found (are you missing a using directive or an assembly reference?)  c:\users\srikanth\documents\visual studio 2012\Projects\WebApplication4\WebApplication4\Default.aspx.cs 76  17  WebApplication4
Error   1   The type or namespace name 'DirectoryServices' does not exist in the namespace 'System' (are you missing an assembly reference?)    c:\users\srikanth\documents\visual studio 2012\Projects\WebApplication4\WebApplication4\Default.aspx.cs 14  14  WebApplication4
Error   10  The type or namespace name 'DirectorySearcher' could not be found (are you missing a using directive or an assembly reference?) c:\users\srikanth\documents\visual studio 2012\Projects\WebApplication4\WebApplication4\Default.aspx.cs 73  17  WebApplication4
Error   11  The type or namespace name 'DirectorySearcher' could not be found (are you missing a using directive or an assembly reference?) c:\users\srikanth\documents\visual studio 2012\Projects\WebApplication4\WebApplication4\Default.aspx.cs 73  48  WebApplication4
Error   8   The type or namespace name 'DirectoryEntry' could not be found (are you missing a using directive or an assembly reference?)    c:\users\srikanth\documents\visual studio 2012\Projects\WebApplication4\WebApplication4\Default.aspx.cs 68  13  WebApplication4
Error   9   The type or namespace name 'DirectoryEntry' could not be found (are you missing a using directive or an assembly reference?)    c:\users\srikanth\documents\visual studio 2012\Projects\WebApplication4\WebApplication4\Default.aspx.cs 68  40  WebApplication4
Error   3   The name 'txtPassword' does not exist in the current context    c:\users\srikanth\documents\visual studio 2012\Projects\WebApplication4\WebApplication4\Default.aspx.cs 39  75  WebApplication4
Error   14  The name 'txtPassword' does not exist in the current context    c:\users\srikanth\documents\visual studio 2012\Projects\WebApplication4\WebApplication4\Default.aspx.cs 97  13  WebApplication4
Error   2   The name 'txtLoginID' does not exist in the current context c:\users\srikanth\documents\visual studio 2012\Projects\WebApplication4\WebApplication4\Default.aspx.cs 29  31  WebApplication4
Error   13  The name 'txtLoginID' does not exist in the current context c:\users\srikanth\documents\visual studio 2012\Projects\WebApplication4\WebApplication4\Default.aspx.cs 96  13  WebApplication4
Error   4   The name 'lblError' does not exist in the current context   c:\users\srikanth\documents\visual studio 2012\Projects\WebApplication4\WebApplication4\Default.aspx.cs 51  21  WebApplication4
Warning 5   'System.Configuration.ConfigurationSettings.AppSettings' is obsolete: '"This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings"'   c:\users\srikanth\documents\visual studio 2012\Projects\WebApplication4\WebApplication4\Default.aspx.cs 33  40  WebApplication4
Warning 6   'System.Configuration.ConfigurationSettings.AppSettings' is obsolete: '"This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings"'   c:\users\srikanth\documents\visual studio 2012\Projects\WebApplication4\WebApplication4\Default.aspx.cs 35  67  WebApplication4
Warning 7   'System.Configuration.ConfigurationSettings.AppSettings' is obsolete: '"This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings"'   c:\users\srikanth\documents\visual studio 2012\Projects\WebApplication4\WebApplication4\Default.aspx.cs 36  62  WebApplication4

解决方案

On the .NET tab in the Add Reference dialog box, click System.DirectoryServices.dll, click Select, and then click OK.


这篇关于你好,每个人都回复我的问题?我使用Asp.net在LDAP中收到错误...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆