IWMSAuthenticationContext.Authenticate (Visual Basic .NET)

The server calls the Authenticate method to verify client credentials.

IWMSAuthenticationContext.Authenticate(  ResponseBlob As Object,
  pUserCtx As IWMSContext,
  pPresentationCtx As IWMSContext,
  pCommandContext As IWMSCommandContext,
  pCallback As IWMSAuthenticationCallback,
  Context As Object
)

Arguments

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

IWMSContextIWMSContext Object (Visual Basic .NET) containing the user context. The plug-in fills the context.

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

IWMSCommandContextIWMSCommandContext Object (Visual Basic .NET) containing the request context. The plug-in fills the context.

IWMSAuthenticationCallbackIWMSAuthenticationCallback Object (Visual Basic .NET). The plug-in calls the IWMSAuthenticationCallback.OnAuthenticateComplete method to send the results of the authentication process to the server.

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 Value

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 IWMSEventLogIWMSEventLog Object (Visual Basic .NET) 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

' This sample implementation of the Authenticate method
' uses a digest authentication scheme.
Public Sub Authenticate(ByVal ResponseBlob As Object, _
    ByVal pUserCtx As IWMSContext, _
    ByVal pPresentationCtx As IWMSContext, _
    ByVal pCommandContext As IWMSCommandContext, _
    ByVal pCallback As IWMSAuthenticationCallback, _
    ByVal Context As Object) _
    Implements IWMSAuthenticationContext.Authenticate

    Dim AUTH_QOP As String = "auth"
    Dim AUTH_REALM As String = "RealmName"

    Dim Enc As Text.Encoding = Text.Encoding.Unicode
    Dim strNonce As String = GenerateNonce()
    Dim Response As Byte()
    Dim Challenge As Byte()

    Try
        Response = ResponseBlob
        If Response.Length = 0 Then
            ' The client requested authentication; prepare the
            ' challenge response to send to the client.
            Challenge = Enc.GetBytes("realm=" & Chr(34) & _
                                     AUTH_REALM & Chr(34) & _
                                     ",qop=" & Chr(34) & _
                                     AUTH_QOP & Chr(34) & _
                                     ",nonce=" & Chr(34) & _
                                     strNonce & Chr(34) & _
                                     ",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.
            Dim CommandReq As IWMSContext
            Dim strResponse As String
            Dim strUserName As String
            Dim strRealm As String
            Dim strURI As String
            Dim strCNonce As String
            Dim strNonceCount As String
            Dim strChalResponse As String
            Dim strCommandName As String
            Dim strUserHash As String
            Dim strA1Hash As String
            Dim strA2Hash As String
            Dim strDigestHash As String
            Dim bOK As Boolean

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

            pCommandContext.GetCommandRequest(CommandReq)
            CommandReq.GetStringValue("@ WMS_COMMAND_NAME", 153, _
                                      strCommandName, _
         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 Then
                bOK = LogonUser("media_client", "", "password", _
                                LOGON32_LOGON_NETWORK, _
                                LOGON32_PROVIDER_DEFAULT, _
                                m_hToken)
                If bOK Then
                    m_Result = _
                      WMS_AUTHENTICATION_RESULT.WMS_AUTHENTICATION_SUCCESS
                Else
                    m_Result = _
                        WMS_AUTHENTICATION_RESULT.WMS_AUTHENTICATION_ERROR
                End If
            Else
                m_Result = _
                       WMS_AUTHENTICATION_RESULT.WMS_AUTHENTICATION_DENIED
            End If

            Challenge = Enc.GetBytes("")
        End If

    Catch e As Exception
        m_Result = WMS_AUTHENTICATION_RESULT.WMS_AUTHENTICATION_ERROR
    Finally
        ' Report the results of the authentication
        ' challenge to the server.
        pCallback.OnAuthenticateComplete(m_Result, Challenge, Context)
    End Try
End Sub

Private Function GetHash(ByVal strIn As String) As String
    Dim Enc As Text.Encoding = Text.Encoding.ASCII
    Dim md5 As MD5 = New MD5CryptoServiceProvider()

    Dim bytInPtr As Byte() = Enc.GetBytes(strIn)
    Dim bytMD5Ptr As Byte() = md5.ComputeHash(bytInPtr)
    Dim strHash As String = BitConverter.ToString(bytMD5Ptr)
    strHash = strHash.Replace("-", "").ToLower()
    Return strHash
End Function

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

Concepts

IWMSAuthenticationContext Object (Visual Basic .NET)