Condividi tramite


Associazione a un oggetto tramite un SID

In Windows Server 2003 è possibile eseguire il binding a un oggetto usando l'identificatore di sicurezza (SID) dell'oggetto e un GUID. Il SID dell'oggetto viene archiviato nell'attributo objectSID. L'associazione a un SID non funziona in Windows 2000.

Il provider LDAP per Servizi di dominio Active Directory fornisce un metodo per l'associazione a un oggetto utilizzando il SID dell'oggetto. Il formato della stringa di associazione è:

LDAP://servername/<SID=XXXXX>

In questo esempio "nomeserver" è il nome del server di directory e "XXXXX" è la rappresentazione di stringa del valore esadecimale del SID. "nomeserver" è facoltativo. La stringa SID viene specificata in un formato in cui ogni carattere nella stringa è la rappresentazione esadecimale di ogni byte del SID. Ad esempio, se la matrice è:

0xAB 0x14 0xE2

la stringa di associazione SID sarà "<SID=AB14E2>". Il funzione ADsEncodeBinaryData non deve essere utilizzata per convertire la matrice SID in una stringa perché precede ogni carattere di byte con una barra rovesciata, che non è un formato di stringa di associazione valido.

La stringa SID può anche assumere il formato "<SID=S-X-X-XX-XXXXXXXXXXXXX-XXXXXXXXXX-XXX>", dove la parte "S-X-X-XX-XXXXXXXXXX-XXXXXXXXXX-XXXXXXXXX-XXXXXXXXX-XXX" corrisponde alla stringa restituita dalla funzione ConvertSidToStringSid.

Quando si utilizza il SID dell'oggetto, alcuni IADs e IADsContainer, metodi e proprietà non sono supportati. Le seguenti proprietà IADs non sono supportate dagli oggetti ottenuti tramite il collegamento utilizzando il SID dell'oggetto:

I seguenti metodi IADsContainer non sono supportati dagli oggetti ottenuti associando tramite il SID dell'oggetto:

Per utilizzare questi metodi e proprietà dopo l'associazione a un oggetto utilizzando il SID dell'oggetto, utilizzare il metodo IADs.Get per recuperare il nome distinto dell'oggetto e quindi usare di nuovo il nome distinto per eseguire di nuovo l'associazione all'oggetto.

Nell'esempio di codice seguente viene illustrato come convertire un objectSid in una stringa associabile.

HRESULT VariantArrayToBytes(VARIANT Variant, 
    LPBYTE *ppBytes, 
    DWORD *pdwBytes);

/********

    GetSIDBindStringFromVariant()

    Converts a SID in VARIANT form, such as an objectSid value, and 
    converts it into a bindable string in the form:

    LDAP://<SID=xxxxxxx...>

    The returned string is allocated with AllocADsMem and must be 
    freed by the caller with FreeADsMem.

*********/

LPWSTR GetSIDBindStringFromVariant(VARIANT vSID)
{
    LPWSTR pwszReturn = NULL;

    if(VT_ARRAY & vSID.vt) 
    {
        HRESULT hr;
        LPBYTE pByte;
        DWORD dwBytes = 0;

        hr = VariantArrayToBytes(vSID, &pByte, &dwBytes);
        if(S_OK == hr)
        {
            // Convert the BYTE array into a string of hex 
            // characters.
            CComBSTR sbstrTemp = "LDAP://<SID=";

            for(DWORD i = 0; i < dwBytes; i++)
            {
                WCHAR wszByte[3];

                swprintf_s(wszByte, L"%02x", pByte[i]);
                sbstrTemp += wszByte;
            }

            sbstrTemp += ">";
            pwszReturn = 
               (LPWSTR)AllocADsMem((sbstrTemp.Length() + 1) * 
                sizeof(WCHAR));
            if(pwszReturn)
            {
                wcscpy_s(pwszReturn, sbstrTemp.m_str);
            }

            FreeADsMem(pByte);
        }
    }

    return pwszReturn;
}

/*********

    VariantArrayToBytes()

    This function converts a VARIANT array into an array of BYTES. 
    This function allocates the buffer using AllocADsMem. The 
    caller must free this memory with FreeADsMem when it is no 
    longer required.

**********/

HRESULT VariantArrayToBytes(VARIANT Variant, 
    LPBYTE *ppBytes, 
    DWORD *pdwBytes)
{
    if(!(Variant.vt & VT_ARRAY) ||
        !Variant.parray ||
        !ppBytes ||
        !pdwBytes)
    {
        return E_INVALIDARG;
    }

    *ppBytes = NULL;
    *pdwBytes = 0;

    HRESULT hr = E_FAIL;
    SAFEARRAY *pArrayVal = NULL;
    CHAR HUGEP *pArray = NULL;
    
    // Retrieve the safe array.
    pArrayVal = Variant.parray;
    DWORD dwBytes = pArrayVal->rgsabound[0].cElements;
    *ppBytes = (LPBYTE)AllocADsMem(dwBytes);
    if(NULL == *ppBytes) 
    {
        return E_OUTOFMEMORY;
    }

    hr = SafeArrayAccessData(pArrayVal, (void HUGEP * FAR *) &pArray);
    if(SUCCEEDED(hr))
    {
        // Copy the bytes to the safe array.
        CopyMemory(*ppBytes, pArray, dwBytes);
        SafeArrayUnaccessData( pArrayVal );
        *pdwBytes = dwBytes;
    }
    
    return hr;
}