首页 新闻 会员 周边

有玩过LDAP的大侠吗?我现在要从AD域里读出来里边的用户加到我的数据库里,但是总说我dc有问题

0
悬赏园豆:50 [待解决问题]

以下是我的代码还有错误信息

String userName = "administrator"; // 用户名称
        String password = "123.com"; // 密码
        String host = "192.168.10.10"; // AD服务器
        String port = "389"; // 端口
        String domain = "@vdidomain.user.com"; // 邮箱的后缀名
        String url = new String("ldap://" + host + ":" + port);
        String user = userName.indexOf(domain) > 0 ? userName : userName + domain;
        Hashtable env = new Hashtable();
        DirContext ctx;
        env.put(Context.SECURITY_AUTHENTICATION, "simple");
        env.put(Context.SECURITY_PRINCIPAL, user); // 不带邮箱后缀名的话,会报错,具体原因还未探究。高手可以解释分享。
        env.put(Context.SECURITY_CREDENTIALS, password);
        env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL, url);
        
        try {
            ctx = new InitialLdapContext(env, null);
          
            Attributes attrs = new BasicAttributes(true);
            Attribute objclass = new BasicAttribute("objectclass");
            // 添加ObjectClass
            String[] attrObjectClassPerson = { "inetOrgPerson", "organizationalPerson", "person", "top" };
            Arrays.sort(attrObjectClassPerson);
            for (String ocp : attrObjectClassPerson) {
              objclass.add(ocp);
            }
            attrs.put(objclass);
            String uid = "zhangsan";
            //CN=ceshi,CN=Users,DC=vdidomain,DC=user,DC=com
            //"cn=Users,dc=vpn,dc=redcross,dc=org,dc=com"
            String userDN = "uid=" + uid + "," + "CN=Users,DC=vdidomain,DC=user,DC=com";
            // 密码处理
            attrs.put("cn", uid);
            attrs.put("sn", uid);
            attrs.put("displayName", "张三");
            attrs.put("mail", "");
            attrs.put("description", "");
            attrs.put("userPassword", "password");
            ctx.createSubcontext(userDN, attrs);
            System.err.println("成功");

错误信息:remaining name 'uid=zhangsan,CN=Users,DC=vdidomain,DC=user,DC=com'

JungleRoco的主页 JungleRoco | 初学一级 | 园豆:152
提问于:2014-06-18 16:12
< >
分享
所有回答(1)
0

贴上我的代码,供楼主参考

 var domainAndUsername = Domain + @"\" + userName;
                var entry = new DirectoryEntry(Path, domainAndUsername, password);
                try
                {
                    //Bind to the native AdsObject to force authentication.            
                    // ReSharper disable once UnusedVariable
                    var obj = entry.NativeObject;
                }
                catch (Exception ex)
                {
                    entry.Dispose();
                    return new Response<DomainUserModel>(ex.Message, false);
                }
                var search = new DirectorySearcher(entry)
                {
                    Filter = "(SAMAccountName=" + userName + ")"
                };
                search.PropertiesToLoad.Add("mail");
                search.PropertiesToLoad.Add("displayName");
                search.PropertiesToLoad.Add("description"); 
                var result = search.FindOne();
                if (result == null)
                {
                    search.Dispose();
                    entry.Dispose();
                    return new Response<DomainUserModel>("未找到该用户",false);
                }
                var mail = result.Properties["mail"][0].ToString();
                var displayName = result.Properties["displayName"][0].ToString();
                var description = result.Properties["description"][0].ToString();
                var domainUser = new DomainUserModel(mail, displayName, description);
                return new Response<DomainUserModel>(domainUser);
影踪派 | 园豆:317 (菜鸟二级) | 2014-06-20 10:01
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册