Condividi tramite


Autenticazione del provider di servizi

Per essere accessibile da Windows Media Gestione dispositivi, un provider di servizi deve ereditare e implementare l'interfaccia IComponentAuthenticate.

Per autenticarsi, un provider di servizi esegue la procedura seguente:

  1. In caso di istanza, crea un nuovo oggetto CSecureChannelServer globale e imposta i valori di certificato e chiave dal file di chiave.
  2. Implementa i metodi IComponentAuthenticate::SACAuth e IComponentAuthenticate::SACGetProtocols semplicemente passando i parametri nel membro CSecureChannelServer globale.
  3. Prima di gestire i metodi di windows Media Gestione dispositivi implementati, il provider di servizi deve verificare l'autenticazione del chiamante chiamando CSecureChannelServer::fIsAuthenticated e se il chiamante non è autenticato.

Questi passaggi sono illustrati negli esempi di C++ seguenti.

Creazione dell'oggetto CSecureChannelServer

CMyServiceProvider::CMyServiceProvider()
{
    HRESULT hr = S_OK;

    // Create the persistent SAC object.
    g_pSAC = new CSecureChannelServer();

    // Set the SAC certificate.
    if (g_pSAC)
    {
        hr = g_pSAC->SetCertificate(
             SAC_CERT_V1,
            (BYTE*)abCert, sizeof(abCert), // SP's certificate.
            (BYTE*)abPVK, sizeof(abPVK)    // SP's key.
        );
    }   
    if (FAILED(hr)) return hr;

    //... Perform other class initialization here ...

    return hr;
}

Implementazione dei metodi IComponentAuthenticate

STDMETHODIMP CMDServiceProvider::SACAuth(
    DWORD   dwProtocolID,
    DWORD   dwPass,
    BYTE   *pbDataIn,
    DWORD   dwDataInLen,
    BYTE  **ppbDataOut,
    DWORD  *pdwDataOutLen)
{
    HRESULT hr = S_OK;

    // Verify that the global CSecureChannelServer member still exists.
    if (!g_pSAC)
        return E_FAIL;

    // Just pass the call to the global SAC member.
    hr = g_pSAC->SACAuth(
        dwProtocolID,
        dwPass,
        pbDataIn, dwDataInLen,
        ppbDataOut, pdwDataOutLen
    );
    return hr;
}

STDMETHODIMP CMDServiceProvider::SACGetProtocols(
    DWORD **ppdwProtocols,
    DWORD  *pdwProtocolCount)
{
    HRESULT hr = E_FAIL;

    if (!g_pSAC)
        return hr;

    hr = g_pSAC->SACGetProtocols(
        ppdwProtocols,
        pdwProtocolCount
    );
    return hr;
}

Verifica dell'autenticazione del chiamante

Nell'esempio di codice seguente viene illustrato un provider di servizi che controlla l'autenticazione del chiamante nell'ambito dell'implementazione dell'interfaccia IMDServiceProvider .

STDMETHODIMP CMyServiceProvider::GetDeviceCount(DWORD * pdwCount)
{
    HRESULT hr = S_OK;
    if (!g_pSAC)
        return E_FAIL;

    if (!(g_pSAC->fIsAuthenticated()))
        return WMDM_E_NOTCERTIFIED;

    *pdwCount = m_DeviceCount;

    return hr;
}

Creazione di un provider di servizi