以下是我的代码还有错误信息
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'
贴上我的代码,供楼主参考
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);