English 中文(简体)
Using JavaScript to get an LDAP multi-valued string attribute
原标题:

I am trying to retrieve an object attribute in Active Directory that appears to be a multi-valued string (See canonicalName). After performing a search:

var conn;
conn.Open =  Provider=ADsDSOObject; ;
var command =  <LDAP://CN=Partitions,CN=Configuration,DC=domain,DC=com>;(&(objectcategory=crossRef)(systemFlags=3));name,nETBIOSName,nCName,canonicalName;onelevel ;
var ado = new ActiveXObject( ADODB.Command );
ado.ActiveConnection = conn;
ado.CommandText = command;
var records = ado.Execute;

and while looping through the recordset:

while (!records.Eof){
  ...

  records.MoveNext();
}

I then try and get the canonicalName:

var cn = records.Fields( canonicalName ).Value;

The problem is that JavaScript doesn t intrepret cn as a string or as an array... if you look at the AD schema for the canonicalName you can see it s configured with isSingleValue = false which is what I believe is causing the problem...

When stepping through the code with Visual Studio I can drill into the string value and the QuickWatch shows cn.value(0) as returning the string value. But when I type that out it doesn t work...

I have tried the usual ways to get the value without luck:

for (var i in cn) { alert(cn[i]); }

and

for (i = 0; i < cn.length; i++) { alert(cn[i]); }

Neither works...

How can I read the value of this object?

最佳回答

I know this is an older question but I figured it out and would like to share.

var ldap = GetObject("LDAP://cn=Group Name, ou=Name, dc=Domain");

var ldapArr = ldap.member.toArray();

for(var x = 0; x < ldapArr.length; x++) {
    WScript.Echo(ldapArr [x]);
    }

Hope that helps to you and anyone else banging their head over it like I have been.

问题回答

Sometimes I ve got the same problem: I can t iterate an object with javascript and however in vb it s done with a simple for each ...

I found a way to iterate in javascript that solved my problem. I hope it is usefull to you:

var enumCn = new Enumerator(cn);
for (; !enumCn.atEnd(); enumCn.moveNext()) {
    var cnItem = enumCn.item();

... do whatever you need with cnItem...

    };
};

I was using ADO Recordset for this, and thought I d help future users passing by with this issue. It utilises the solution by Xiazer.

I m using Internet Explorer 11 and a localhost Apache DS Explorer LDAP Server. ANd JQuery of course.

Just put this in any HTML file and open in Internet Explorer.

$(document).ready(function(){
    console.log("Starting");
    showUsers();
    console.log("Done")
});

function GetFieldValue(objField){
    var result = "";
    if (objField.Value == null){
    }
    else{
        if (objField.type == 12){
          var ldapArr = objField.Value.toArray();
          result = ldapArr[0];
        }
        else{
          result = objField.Value;
        }
    }
    return result;
}//GetFieldValue

function showUsers(){
  console.log("showUsers");
  var strAttributes, strFilter, SrchCriteria, strQuery;

  strAttributes = "cn,sn,entryDN"
  SrchCriteria="(objectClass=iNetOrgPerson)"

  var ADOCommand = new ActiveXObject("ADODB.Command");
  var ADOConnection = new ActiveXObject("ADODB.Connection");
  var ADORecordSet = new ActiveXObject("ADODB.RecordSet");
  ADOConnection.Provider = "ADsDSOObject";
  ADOConnection.Properties("User ID") = "uid=admin,ou=system"
  ADOConnection.Properties("Password") = "secret"
  strDomainName = "localhost:10389/dc=mijnldap,dc=local"
  strBase = "<LDAP://" + strDomainName + ">";

  ADOConnection.Open("Active Directory Provider");
  ADOCommand.ActiveConnection = ADOConnection;
  strFilter = SrchCriteria;
  strQuery = strBase + ";" + strFilter + ";" + strAttributes + ";subtree";
  strQuery = strQuery;

  console.log(strQuery);

  ADOCommand.CommandText = strQuery;
  ADORecordSet = ADOCommand.Execute;
  console.log("Query executed");

  if (ADORecordSet.RecordCount > 0) {
    ADORecordSet.MoveFirst;
    while (! ADORecordSet.EOF){
      console.log (GetFieldValue(ADORecordSet.Fields("sn")));
      console.log (GetFieldValue(ADORecordSet.Fields("cn")));
      console.log (GetFieldValue(ADORecordSet.Fields("entryDN")));
      ADORecordSet.MoveNext;
    }
  }//if 
  ADORecordSet.close

} //showUsers




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签