public string createUserLdap(User user) { securityBoundary = new SecurityBoundary(); try { return securityBoundary.createUserLdap(user); } catch (BusinessException e) { throw new WebFaultException<GeneralResponse> (new GeneralResponse { message = e.Message, status = GeneralResponse.STATUS_ERROR, code = "400" }, HttpStatusCode.BadRequest); } catch (PlatformException e) { throw new WebFaultException<GeneralResponse> (new GeneralResponse { message = e.Message, status = GeneralResponse.STATUS_ERROR, code = "500" }, HttpStatusCode.BadRequest); } }
public string createUserLdap(User user) { ldapId = new LdapDirectoryIdentifier(HOST, PORT); network = new NetworkCredential(ADMIN, ADMIN_PASS); using (LdapConnection connection = new LdapConnection(ldapId, network, AuthType.Basic)) { try { string[] objectClass = new string[] { "top", "inetOrgPerson", "organizationalPerson", "person" }; connection.SessionOptions.SecureSocketLayer = false; connection.SessionOptions.ProtocolVersion = 3; String dn = DN_CREATE.Replace("{0}", user.email); DirectoryAttributeCollection collection = new DirectoryAttributeCollection() { new DirectoryAttribute("objectclass", objectClass), new DirectoryAttribute("uid",user.email), new DirectoryAttribute("sn", user.lastName), new DirectoryAttribute("cn", user.userName), new DirectoryAttribute("employeeNumber", user.userId), new DirectoryAttribute("departmentNumber", user.userGroup), new DirectoryAttribute("userPassword", user.password) }; AddRequest addMe = new AddRequest(dn, "inetOrgPerson"); addMe.Attributes.AddRange(collection); connection.Bind(); connection.SendRequest(addMe); return "OK"; } catch (LdapException ex) { throw new BusinessException("Ldap error: " + ex.Message); } catch (Exception e) { throw new PlatformException("Ldap error: " + e.Message); } } }
private User queryLdap(string email) { string ldapFilter = "(objectClass=person)"; string ldapTarget = DN.Replace("{0}", email); User user = new User(); network = new NetworkCredential(ADMIN, ADMIN_PASS); ldapId = new LdapDirectoryIdentifier(HOST, PORT); using (LdapConnection connection = new LdapConnection(ldapId, network, AuthType.Basic)) { try { connection.SessionOptions.SecureSocketLayer = false; connection.SessionOptions.ProtocolVersion = 3; connection.Bind(); SearchRequest searchRequest = new SearchRequest(ldapTarget, ldapFilter, SearchScope.Subtree, "*"); SearchResponse searchResponse = (SearchResponse)connection.SendRequest(searchRequest); SearchResultEntry entry = searchResponse.Entries[0]; user.email = email; user.userId = entry.Attributes["employeeNumber"][0].ToString(); user.userName = entry.Attributes["cn"][0].ToString(); user.lastName = entry.Attributes["sn"][0].ToString(); user.userGroup = entry.Attributes["departmentNumber"][0].ToString(); connection.Dispose(); return user; } catch (LdapException ex) { throw new BusinessException(ex.Message); } catch (Exception e) { throw new PlatformException(e.Message); } } }