Udostępnij przez


Implementowanie menedżera IMFNetCredentialManager

W metodzie IMFNetCredentialManager::BeginGetCredentials wykonaj następujące czynności.

  1. Jeśli nie masz już wskaźnika IMFNetCredentialCache, wywołaj MFCreateCredentialCache, aby utworzyć obiekt pamięci podręcznej poświadczeń. Zapisz ten wskaźnik.
  2. Wywołaj IMFNetCredentialCache::GetCredential. Ustaw flagi w parametrze dwAuthenticationFlags w następujący sposób:
    • Jeśli hrOp element członkowski struktury MFNetCredentialManagerGetParam jest równy NS_E_PROXY_ACCESSDENIED, ustaw flagę MFNET_AUTHENTICATION_PROXY.
    • Jeśli fClearTextPackage jest TRUE, ustaw flagę MFNET_AUTHENTICATION_CLEAR_TEXT.
    • Jeśli fAllowLoggedOnUser jest TRUE, ustaw flagę MFNET_AUTHENTICATION_LOGGED_ON_USER.
  3. Metoda GetCredential zwraca wskaźnik IMFNetCredential i prawdopodobnie flagę REQUIRE_PROMPT. Zapisz wskaźnik IMFNetCredential.
  4. Jeśli GetCredential nie zwraca flagi REQUIRE_PROMPT, to koniec. Przejdź do kroku 9.
  5. W przeciwnym razie, jeśli getCredential zwróci flagę REQUIRE_PROMPT, musisz poprosić użytkownika o podanie jego nazwy użytkownika i hasła.
  6. Jeśli fClearTextPackage jest false, szyfruj poświadczenia.
  7. Wywołaj IMFNetCredential::SetUser i IMFNetCredential::SetPassword, aby ustawić nazwę użytkownika i hasło w obiekcie credentials.
  8. Opcjonalnie wywołaj IMFNetCredentialCache::SetUserOptions, aby zaktualizować obiekt pamięci podręcznej poświadczeń zgodnie z preferencjami użytkownika dotyczącymi przechowywania i wysyłania poświadczeń.
  9. Aby wywołać wywołanie zwrotne IMFAsyncCallback, użyj MFInvokeCallback.

W metodzie IMFNetCredentialManager::EndGetCredentials zwróć wskaźnik IMFNetCredential uzyskany w metodzie BeginGetCredentials.

W metodzie IMFNetCredentialManager::SetGood przekaż parametry wejściowe bezpośrednio do metody IMFNetCredentialCache::SetGood. Powiadamia pamięć podręczną poświadczeń, czy poświadczenia zostały zaakceptowane przez serwer.

Jeśli musisz wyświetlić monit użytkownika (krok 5) lub zaszyfrować poświadczenia (krok 6), należy to zrobić w wątku kolejki roboczej, aby nie blokować potoku programu Microsoft Media Foundation. Wywołaj MFPutWorkItem, a następnie wykonaj pozostałe kroki wewnątrz callbacku kolejki roboczej.

Notatka

Należy pamiętać, że BeginGetCredentials mogą być wywoływane podczas tworzenia źródła sieci. W związku z tym, jeśli utworzysz źródło sieci przy pomocy synchronicznej metody IMFSourceResolver::CreateObjectFromURL, to wątek, który ją wywołuje, może zostać zablokowany podczas uzyskiwania poświadczeń. Dlatego zaleca się użycie asynchronicznej metody IMFSourceResolver::BeginCreateObjectFromURL.

 

Przykład

W tym przykładzie przedstawiono jeden typ zachowania, które może zapewnić menedżer poświadczeń.

Oto deklaracja klasy, która implementuje IMFNetCredentialManager.

class CCredentialManager : public IMFNetCredentialManager, IMFAsyncCallback 
{
    long                    m_cRef;
    IMFNetCredentialCache   *m_pCredentialCache;

public:
    CCredentialManager () : m_cRef(1), m_pCredentialCache(NULL)
    { 
    }
    ~CCredentialManager()
    {
        SafeRelease(&m_pCredentialCache);
    }

    STDMETHODIMP QueryInterface(REFIID riid, void** ppv)
    {
        static const QITAB qit[] = 
        {
            QITABENT(CCredentialManager, IMFNetCredentialManager),
            QITABENT(CCredentialManager, IMFAsyncCallback),
            { 0 }
        };
        return QISearch(this, qit, riid, ppv);
    }      

    STDMETHODIMP_(ULONG) AddRef()
    {
        return InterlockedIncrement(&m_cRef);
    }

    STDMETHODIMP_(ULONG) Release()
    {
        LONG cRef = InterlockedDecrement(&m_cRef);
        if (cRef == 0)
        {
            delete this;
        }
        return cRef;
    }

    STDMETHODIMP BeginGetCredentials(
        MFNetCredentialManagerGetParam* pParam,
        IMFAsyncCallback* pCallback,
        IUnknown* pState
        );

    STDMETHODIMP EndGetCredentials(
        IMFAsyncResult* pResult, 
        IMFNetCredential** ppCred);

    STDMETHODIMP SetGood(IMFNetCredential* pCred, BOOL fGood)
    {
        if (!pCred)
        {
            return E_POINTER;
        }

        return m_pCredentialCache->SetGood(pCred, fGood);
    }


    STDMETHODIMP GetParameters(DWORD* pdwFlags, DWORD* pdwQueue)
    {
        return E_NOTIMPL;
    }

    STDMETHODIMP Invoke(IMFAsyncResult* pResult);
};

Aby śledzić stan operacji BeginGetCredentials, klasa używa następującego obiektu pomocniczego:

// Holds state information for the GetCredentials operation, so that work can 
// be moved to a work-queue thread.

struct CredentialOp : public IUnknown
{
    long                m_cRef;
    IMFNetCredential    *m_pCredential;
    DWORD               m_dwFlags;

    CredentialOp(IMFNetCredential *pCredential) 
        : m_cRef(1), m_dwFlags(0), m_pCredential(pCredential)
    {
        m_pCredential->AddRef();
    }

    ~CredentialOp()
    {
        SafeRelease(&m_pCredential);
    }

    STDMETHODIMP QueryInterface(REFIID riid, void** ppv)
    {
        static const QITAB qit[] = 
        {
            QITABENT(CredentialOp, IUnknown),
            { 0 }
        };
        return QISearch(this, qit, riid, ppv);
    }      

    STDMETHODIMP_(ULONG) AddRef()
    {
        return InterlockedIncrement(&m_cRef);
    }

    STDMETHODIMP_(ULONG) Release()
    {
        LONG cRef = InterlockedDecrement(&m_cRef);
        if (cRef == 0)
        {
            delete this;
        }
        return cRef;
    }
};

Metoda BeginGetCredentials tworzy pamięć podręczną poświadczeń i pobiera wskaźnik IMFNetCredential. Jeśli użytkownik musi otrzymać monit (wskazywany przez flagę REQUIRE_PROMPT), metoda wywołuje MFPutWorkItem w celu kolejkowania nowego elementu roboczego.

STDMETHODIMP CCredentialManager::BeginGetCredentials(
    MFNetCredentialManagerGetParam* pParam,
    IMFAsyncCallback* pCallback,
    IUnknown* pState
    )
{
    if (!pParam || !pCallback)
    {
        return E_POINTER;
    }

    DWORD dwAuthenticationFlags = 0;
    DWORD dwRequirementFlags = 0;

    if (pParam->hrOp == NS_E_PROXY_ACCESSDENIED)
    {
        dwAuthenticationFlags |= MFNET_AUTHENTICATION_PROXY;
    }

    if (pParam->fAllowLoggedOnUser)
    {
        dwAuthenticationFlags |= MFNET_AUTHENTICATION_LOGGED_ON_USER;
    }

    if (pParam->fClearTextPackage)
    {
        dwAuthenticationFlags |= MFNET_AUTHENTICATION_CLEAR_TEXT;
    }

    IMFNetCredential *pCredential =  NULL;
    IMFAsyncResult* pResult = NULL;

    HRESULT hr = S_OK;

    if (m_pCredentialCache == NULL)
    {
        hr = MFCreateCredentialCache(&m_pCredentialCache);
        if (FAILED(hr))
        {
            goto done;
        }
    }

    hr = m_pCredentialCache->GetCredential(
        pParam->pszUrl, 
        pParam->pszRealm, 
        dwAuthenticationFlags, 
        &pCredential, 
        &dwRequirementFlags
        );

    if (FAILED(hr))
    {
        goto done;
    }

    if( ( dwRequirementFlags & REQUIRE_PROMPT ) == 0 )
    {
        // The credential is good to use. Prompting the user is not required.
        hr = S_OK;
        goto done;
    }

    // The credential requires prompting the user. 
    CredentialOp *pOp = new (std::nothrow) CredentialOp(pCredential);

    if (pOp == NULL)
    {
        hr = E_OUTOFMEMORY;
        goto done;
    }

    // Set flags. Use these to inform the user if the credentials will
    // be sent in plaintext or saved in the credential cache.

    if (pParam->fClearTextPackage)
    {
        // Notify the user that credentials will be sent in plaintext.
        pOp->m_dwFlags |= MFNET_CREDENTIAL_ALLOW_CLEAR_TEXT;
    }

    if(dwRequirementFlags & REQUIRE_SAVE_SELECTED )
    {
        // Credentials will be saved in the cache by default.
        pOp->m_dwFlags |= MFNET_CREDENTIAL_SAVE;
    }

    // NOTE: The application should enable to user to deselect these two flags;
    // for example, through check boxes in the prompt dialog.


    // Now queue the work item.

    hr = MFCreateAsyncResult(pOp, pCallback, pState, &pResult);
    if (FAILED(hr))
    {
        goto done;
    }

    hr = MFPutWorkItem(MFASYNC_CALLBACK_QUEUE_LONG_FUNCTION, this, pResult);

done:
    SafeRelease(&pResult);
    SafeRelease(&pCredential);
    SafeRelease(&pOp);
    return hr;
}

Wątek kolejki roboczej wywołuje Invoke, który najpierw poprosi użytkownika, a następnie wywoła MFInvokeCallback, aby uruchomić wskaźnik wywołania zwrotnego podany w BeginGetCredentials.

STDMETHODIMP CCredentialManager::Invoke(IMFAsyncResult* pResult)
{
    IUnknown *pState = NULL;
    IMFAsyncResult *pGetCredentialsResult = NULL;
    IUnknown *pOpState = NULL;

    CredentialOp *pOp = NULL;   // not AddRef'd

    HRESULT hr = pResult->GetState(&pState);

    if (SUCCEEDED(hr))
    {
        hr = pState->QueryInterface(IID_PPV_ARGS(&pGetCredentialsResult));
    }

    if (SUCCEEDED(hr))
    {
        hr = pGetCredentialsResult->GetObject(&pOpState);
    }

    if (SUCCEEDED(hr))
    {
        pOp = static_cast<CredentialOp*>(pOpState);

        // Display a dialog for the user to enter user name and password.
        hr = PromptUserCredentials(pOp);
    }

    if (SUCCEEDED(hr) && m_pCredentialCache)
    {
        // Update with options set by the user.
        hr = m_pCredentialCache->SetUserOptions(
            pOp->m_pCredential, 
            pOp->m_dwFlags
            );
    }

    if (pGetCredentialsResult)
    {
        pGetCredentialsResult->SetStatus(hr);
        MFInvokeCallback(pGetCredentialsResult);
    }

    SafeRelease(&pState);
    SafeRelease(&pGetCredentialsResult);
    SafeRelease(&pOpState);
    return S_OK;
}

Metoda EndGetCredentials kończy operację, zwracając wskaźnik IMFNetCredential do wywołującego.

STDMETHODIMP CCredentialManager::EndGetCredentials(
    IMFAsyncResult* pResult, 
    IMFNetCredential** ppCred
    )
{
    if (!pResult || !ppCred)
    {
        return E_POINTER;
    }

    *ppCred = NULL;

    IUnknown *pUnk = NULL;

    // Check the result of the asynchronous operation.
    HRESULT hr = pResult->GetStatus();

    if (FAILED(hr))
    {
        // The operation failed.
        goto done;
    }

    hr = pResult->GetObject(&pUnk);
    if (FAILED(hr))
    {
        goto done;
    }

    CredentialOp *pOp = static_cast<CredentialOp*>(pUnk);

    *ppCred = pOp->m_pCredential;
    pOp->m_pCredential = NULL;

done:
    SafeRelease(&pUnk);
    return hr;
}

Uwierzytelnianie źródła sieci