C#使用Domino的LDAP服务验证用户

首先,Domino的LDAP服务配置为不能匿名访问,不然就没意义了。

 

安全性中,internet认证设置为:较多名称变换,较低安全性。

这样的话用户名可以使用Domino个人文档中的:名+空格+姓、简称、Email地址

如果不需要的话也可以不这样设置。

密码为:Internet密码

 

默认情况下Domino的LDAP属性没有uid(简称),需要自己添加下。

 

原理:使用输入的用户名密码执行一个LDAP查询,如果没有抛出异常,说明用户名密码正确。

 

  1 using  System.DirectoryServices;
  2 using  System.Configuration;
  3 using  System.Text;
  4
  5      public   class  LDAPHelper
  6      {
  7
  8        private string dominoServer;
  9        /// <summary>
 10        /// domino服务器地址
 11        /// </summary>

 12        public string DominoServer
 13        {
 14            get return dominoServer; }
 15            set { dominoServer = value; }
 16        }

 17        private string ldapServer;
 18        /// <summary>
 19        /// ldap服务器地址
 20        /// </summary>

 21        public string LdapServer
 22        {
 23            get return ldapServer; }
 24            set { ldapServer = value; }
 25        }

 26
 27        private string userName;
 28        /// <summary>
 29        /// 用户名
 30        /// </summary>

 31        public string UserName
 32        {
 33            get return userName; }
 34            set { userName = value; }
 35        }

 36
 37        private string password;
 38        /// <summary>
 39        /// 密码
 40        /// </summary>

 41        public string Password
 42        {
 43            get return password; }
 44            set { password = value; }
 45        }

 46        /// <summary>
 47        /// 实例化
 48        /// </summary>
 49        /// <param name="userName">用户名</param>
 50        /// <param name="password">密码</param>

 51        public LDAPHelper(string userName, string password)
 52        {
 53            try
 54            {
 55                //this.LdapServer = ConfigurationManager.AppSettings["LDAPServer"].ToString();
 56
 57                //this.DominoServer = ConfigurationManager.AppSettings["DominoServer"].ToString();
 58
 59                this.LdapServer = "192.176.0.46";
 60
 61                this.DominoServer= "192.176.0.46"
 62
 63                this.UserName = userName;
 64                this.Password = password;
 65            }

 66            catch
 67            {
 68                throw;
 69            }

 70        }

 71
 72        /// <summary>
 73        /// 通过domino LDAP服务验证用户名密码是否正确
 74        /// </summary>
 75        /// <param name="userName">用户名</param>
 76        /// <param name="password">密码</param>
 77        /// <returns></returns>

 78        public bool VerifyUser()
 79        {
 80            try
 81            {
 82                DirectorySearcher searcher = prepareSearcher();
 83
 84                //执行以下方法时没抛出异常说明用户名密码正确
 85                SearchResult rs = searcher.FindOne();
 86
 87                return true;
 88            }

 89            catch
 90            {
 91                return false;
 92            }

 93        }

 94
 95        /// <summary>
 96        /// Prepare DirectorySearcher
 97        /// </summary>
 98        /// <returns>DirectorySearcher</returns>

 99        private DirectorySearcher prepareSearcher()
100        {
101
102            DirectoryEntry root = new DirectoryEntry(this.LdapServer, this.UserName, this.Password, AuthenticationTypes.None);
103            DirectorySearcher searcher = new DirectorySearcher(root);
104
105            //LDAP搜索的过滤条件,cn为用户名,uid为用户简称
106
107            searcher.Filter = "(&(objectClass=dominoPerson)(|(cn=" + this.UserName +
108
109")(uid=" + this.UserName + ")(mail=" + this.UserName + ")))";
110
111            
112            //searcher.Filter = "(&(objectClass=dominoPerson)(cn=" + this.UserName +"))";
113            return searcher;
114
115        }

116    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值