Share via


IWMSAuthenticationContext.Authenticate (C#)

banner art

Previous Next

IWMSAuthenticationContext.Authenticate (C#)

The server calls the Authenticate method to verify client credentials.

Syntax

  

Parameters

ResponseBlob

object containing client authentication data passed from the server. The exact nature of the data is specific to the authentication protocol being used.

pUserCtx

IWMSContext object containing the user context. The plug-in fills the context.

pPresentationCtx

IWMSContext object containing the presentation context. The plug-in fills the context.

pCommandContext

IWMSCommandContext object containing the request context. The plug-in fills the context.

pCallback

IWMSAuthenticationCallback object containing the callback function. The plug-in calls the IWMSAuthenticationCallback.OnAuthenticateComplete method to send the results of the authentication process to the server.

Context

object containing a value defined by the server to identify which call to Authenticate the plug-in is responding to when it calls IWMSAuthenticationCallback.OnAuthenticateComplete. You must pass this value back unaltered.

Return Values

This method does not return a value. To report an error, the plug-in can throw a COMException object to the server. If the plug-in uses the IWMSEventLog object to log error information, it is recommended that it throw NS_E_PLUGIN_ERROR_REPORTED (0xC00D157D). Typically, the server attempts to make plug-in error information available to the server object model, the Windows Event Viewer, and the troubleshooting list in the details pane of the Windows Media Services MMC. However, if the plug-in uses the IWMSEventLog object to send custom error information to the Windows Event Viewer, throwing NS_E_PLUGIN_ERROR_REPORTED stops the server from also logging to the event viewer. For more information about plug-in error information, see Identifying Plug-in Errors.

Remarks

The server calls the Authenticate method. The plug-in must call the IWMSAuthenticationCallback.OnAuthenticateComplete method.

Example Code

// This sample implementation of the Authenticate method
// uses a digest authentication scheme.
public void Authenticate(
    object ResponseBlob,
    IWMSContext pUserCtx,
    IWMSContext pPresentationCtx,
    IWMSCommandContext pCommandContext,
    IWMSAuthenticationCallback pCallback,
    object Context)
{
    string AUTH_QOP = "auth";
    string AUTH_REALM = "RealmName";

    Encoding Enc = Encoding.Unicode;
    string strNonce = GenerateNonce();
    byte[] Response;
    byte[] Challenge = Enc.GetBytes("");

    try
    {
        Response = (byte[])ResponseBlob;
        if( Response.Length == 0 )
        {
            // The client requested authentication; prepare the
            // challenge response to send to the client.
            Challenge = Enc.GetBytes("realm=\"" + AUTH_REALM +
                                   "\",qop=\"" + AUTH_QOP +
                                   "\",nonce=\"" + strNonce +
                                   "\",charset=utf-8,algorithm=MD5-sess");

            m_Result =
                    WMS_AUTHENTICATION_RESULT.WMS_AUTHENTICATION_CONTINUE;
        }
        else
        {
            // The client has responded to the authentication
            // challenge; verify the client credentials.
            IWMSContext CommandReq;
            string strResponse;
            string strUserName;
            string strRealm;
            string strURI;
            string strCNonce;
            string strNonceCount;
            string strChalResponse;
            string strCommandName;
            string strUserHash;
            string strA1Hash;
            string strA2Hash;
            string strDigestHash;
            bool bOK;

            strResponse = Enc.GetString(Response);
            SplitResponse(strResponse, out strUserName, out strRealm,
                          out strURI, out strCNonce, out strNonceCount,
                          out strChalResponse);

            pCommandContext.GetCommandRequest(out CommandReq);
            CommandReq.GetStringValue("@ WMS_COMMAND_NAME", 153,
                                      out strCommandName,
                                      Convert.ToInt32(
       WMS_CONTEXT_OPTIONS.WMS_CONTEXT_GET_PROPERTY_STRING_BY_REFERENCE));

            // Retrieve the user hash from the user database.
            strUserHash = GetUserHash(strUserName);
            strA1Hash = GetHash(strUserHash + ":" +
                                strNonce + ":" + strCNonce);
            strA2Hash = GetHash(strCommandName + ":" + strURI);
            strDigestHash = GetHash(strA1Hash + ":" + strNonce +
                                    ":" + strNonceCount + ":" +
                                    strCNonce + ":" + AUTH_QOP +
                                    ":" + strA2Hash);

            // If the computed digest hash is equal to the
            // client response, the user can be authenticated.
            if( strDigestHash == strChalResponse )
            {
                bOK = LogonUser("media_client", "", "password",
                    LOGON32_LOGON_NETWORK,
                    LOGON32_PROVIDER_DEFAULT,
                    out m_hToken);
                if( bOK )
                    m_Result =
                     WMS_AUTHENTICATION_RESULT.WMS_AUTHENTICATION_SUCCESS;
                else
                    m_Result =
                       WMS_AUTHENTICATION_RESULT.WMS_AUTHENTICATION_ERROR;
            }
            else
            {
                m_Result =
                      WMS_AUTHENTICATION_RESULT.WMS_AUTHENTICATION_DENIED;
            }
        }
    }
    catch(Exception e)
    {
        // TODO: Handle exceptions.
    }
    finally
    {
        // Report the results of the authentication
        // challenge to the server.
        pCallback.OnAuthenticateComplete(m_Result, Challenge, Context);
    }
}

Requirements

Reference: Add a reference to Microsoft.WindowsMediaServices.

Namespace: Microsoft.WindowsMediaServices.Interop.

Assembly: Microsoft.WindowsMediaServices.dll.

Library: WMSServerTypeLib.dll.

Platform: Windows Server 2003, Enterprise Edition; Windows Server 2003, Datacenter Edition; Windows Server 2008.

See Also

Previous Next