Bagikan melalui


Mengautentikasi Aplikasi

Langkah pertama yang harus dilakukan aplikasi Anda adalah autentikasi. Autentikasi memverifikasi identitas aplikasi ke Windows Media Device Manager. Setelah mengautentikasi aplikasi, Anda dapat memanggil QueryInterface untuk mendapatkan antarmuka IWMDeviceManager, yang dapat diminta untuk antarmuka lain yang diperlukan, dan yang dapat diminta sendiri untuk semua antarmuka lainnya. Autentikasi hanya perlu berlangsung sekali, saat startup.

Untuk mengautentikasi aplikasi Anda, lakukan langkah-langkah berikut:

  1. Buat bersama objek MediaDevMgr (id kelas MediaDevMgr), dan minta antarmuka IComponentAuthenticate.
  2. Buat objek CSecureChannelClient untuk menangani autentikasi.
  3. Serahkan kunci aplikasi Anda dan sebuah sertifikat transfer ke objek saluran aman. Anda dapat menggunakan kunci/sertifikat dummy yang ditampilkan dalam contoh kode berikut untuk mendapatkan fungsionalitas dasar dari fungsi SDK. Namun, untuk mendapatkan fungsionalitas penuh (penting untuk meneruskan file ke dan dari perangkat), Anda harus meminta kunci dan sertifikat dari Microsoft seperti yang dijelaskan dalam Tools for Development.
  4. Teruskan antarmuka IComponentAuthenticate yang Anda buat di langkah 1 ke objek saluran aman.
  5. Panggil CSecureChannelClient::Authenticate untuk mengautentikasi aplikasi Anda.
  6. Kueri IComponentAuthenticate untuk antarmuka IWMDeviceManager.

Langkah-langkah ini diperlihatkan dalam kode C++ berikut.

HRESULT CWMDMController::Authenticate()
{
    // Use a dummy key/certificate pair to allow basic functionality.
    // An authentic keypair is required for full SDK functionality.
    BYTE abPVK[] = {0x00};
    BYTE abCert[] = {0x00};
    HRESULT hr;
    CComPtr<IComponentAuthenticate> pAuth;

    // Create the WMDM object and acquire 
    // its authentication interface.
    hr = CoCreateInstance(
        __uuidof(MediaDevMgr),
        NULL,
        CLSCTX_INPROC_SERVER,
        __uuidof(IComponentAuthenticate),
        (void**)&pAuth);

    if (FAILED(hr)) return hr;

    // Create the secure channel client class needed to authenticate the application.
    // We'll use a global member variable to hold the secure channel client
    // in case we need to handle encryption, decryption, or MAC verification
    // during this session.
    m_pSAC = new CSecureChannelClient;
    if (m_pSAC == NULL) return E_FAIL;

    // Send the application's transfer certificate and the associated 
    // private key to the secure channel client.
    hr = m_pSAC->SetCertificate(
        SAC_CERT_V1,
        (BYTE *)abCert, sizeof(abCert),
        (BYTE *)abPVK,  sizeof(abPVK));
    if (FAILED(hr)) return hr;
            
    // Send the authentication interface we created to the secure channel 
    // client and authenticate the application with the V1 protocol.
    // (This is the only protocol currently supported.)
    m_pSAC->SetInterface(pAuth);
    hr = m_pSAC->Authenticate(SAC_PROTOCOL_V1);
    if (FAILED(hr)) return hr;

    // Authentication succeeded, so we can use WMDM.
    // Query for the root WMDM interface.
    hr = pAuth->QueryInterface( __uuidof(IWMDeviceManager), (void**)&m_IWMDMDeviceMgr);

    return hr;
}

Membuat Aplikasi Windows Media Device Manager