Binding to an AD object using SID

Binding to an AD object using SID

Post by Georg » Wed, 29 Jan 2003 17:12:19



Greetings,

I wish to bind to an AD object using its SID. I have seen a sample in MSDN
which binds using this sytax: LDAP://<SID=xxxx>. My problem is that the SID
I have is in binary form. I tried encoding it using ADsEncodeBinaryData, but
ADSOpenObject does not accept it.

So my question is, how do I convert a binary SID to a textutal
representation that I can use with  LDAP://<SID=xxxx>?

Thanks,

James

 
 
 

Binding to an AD object using SID

Post by Georg » Fri, 31 Jan 2003 19:22:00


Thanks Max, that worked ;)


Quote:> You need to use the Hexstring form of the SID.

> The ADsSecurity.dll has an object called ADsSID, it will convert sids from

one form to another.
Quote:

> Below is a VBS way to build a hexstring, a C function that creates a hex

string and a VBS example of how to use ADsSID.
Quote:

> Once you have the hexstring form of the SID, your code should work.

> Sincerely,
> Max Vaughn [MS]
> Microsoft Developer Support

> Disclaimer: This posting is provided "AS IS" with no warranties, and

confers no rights. You assume all risk for your use.
Quote:

> VBS:
> '
> ' Build an HexString SID
> ' oSID is an array of bytes that represent the SID
> ' could be the array retruned by IADs::Get("ObjectSid")
> '
> function Get_HexString( oSID )
> outStr = ""
> for i = 0 to Ubound(oSid)
>   b = hex(ascb(midb(oSid,i+1,1)))
>   if( len(b) = 1 ) then b = "0" & b
>   outStr = outStr & b
> next
> Get_HexString = outStr
> end function

> C++:
> //
> // MakeHexString
> // must use free to release the buffer allocated into pstrHexStr
> //
> HRESULT MakeHexString( LPBYTE lpByte, DWORD cbByte, char **pstrHexStr)
> {
>    HRESULT hr;
>    hr = S_OK;
>    DWORD i;
>    BYTE bVal;
>    ULONG val, mVal;
>    *pstrHexStr = (char *)malloc( (cbByte * 2 ) * sizeof(char) + 1 );
>    char buffer[10];
>    DWORD j;
>    j = 0;
>    for( i = 0;i < cbByte;i++)
>    {
>        bVal = lpByte[i];
>        val = bVal / 16;
>        mVal = bVal % 16;
>        _itoa( val, buffer, 16 );
>        (*pstrHexStr)[j] = buffer[0];
>        _itoa( mVal, buffer, 16);
>        (*pstrHexStr)[j+1] = buffer[0];
>        j = j + 2;

>    }
>    (*pstrHexStr)[j] = NULL;
>    return hr;
> }

> Using ADsSID:

> '
> 'Constants
> '
> CONST ADS_SID_RAW = 0
> CONST ADS_SID_HEXSTRING = 1
> CONST ADS_SID_SAM = 2
> CONST ADS_SID_WINNT_PATH = 5
> CONST ADS_SID_ACTIVE_DIRECTORY_PATH = 6
> '
> ' Use ADsSID
> '
> dim oCnv : set oCnv = CreateObject("ADsSID")
> oCnv.SetAs ADS_SID_WINNT_PATH, "WinNT://Mydomain/MyUser,user"
> WScript.Echo oCnv.GetAs(ADS_SID_HEXSTRING)