asp.net验证ad用户的方法

来源:互联网 发布:java 数据类型取值范围 编辑:程序博客网 时间:2024/05/01 09:30

1.要使用DirectoryEntry必须首先添加引用System.DirectoryServices

2.DirectoryEntry 类可封装 Active Directory 域服务层次结构中的节点或对象。 使用此类绑定到对象、读取属性和更新特性。

3.DirectoryEntry 与帮助器类一起为生存期管理和导航方法提供支持,包括创建、删除、重命名、移动子节点和枚举子级。

4.使用 DirectorySearcher 类可对 Active Directory 域服务层次结构执行查询。

5. LDAP 是系统提供的唯一一种支持搜索的 Active Directory 服务接口 (ADSI) 提供程序。

6.通过 DirectorySearcher 搜索 Active Directory 域服务层次结构将返回 SearchResult 的实例,这些实例包含在 SearchResultCollection类的实例中。

代码实例:

//验证ad中用户名和密码是否正确

//dotnet 4.0

//使用vs2010开发

  public class AuthenticationADUser
    {
        //srvr = ldap server, e.g. LDAP://zsy.com
        //usr = user name e.g.  zhangsan
        //pwd = user password e.g. 123
        public bool IsAuthenticated(string srvr, string usr, string pwd)
        {
            bool authenticated = false;
            try
            {                
                DirectoryEntry entry = new DirectoryEntry(srvr, usr, pwd, AuthenticationTypes.Secure);
                object nativeObject = entry.NativeObject;
                authenticated = true;
            }         
            catch (Exception ex)
            {
                //not authenticated due to some other exception [this is optional] 
            }
            return authenticated;
        }      
    }

0 0
原创粉丝点击