IWMSEventAuthorizationPlugin.AuthorizeEvent (C#)

banner art

Previous Next

IWMSEventAuthorizationPlugin.AuthorizeEvent (C#)

The AuthorizeEvent method is used by the plug-in to authorize the event requested by the server.

Syntax

  void 
  

Parameters

pEvent

[in] Reference to a WMS_EVENT structure containing the event to be authorized.

pUserCtx

[in] IWMSContext object containing the user context.

pPresentationCtx

[in] IWMSContext object containing the presentation context.

pCommandCtx

[in] IWMSCommandContext object containing the command context.

pCallback

[in] IWMSEventAuthorizationCallback object used by the plug-in to call IWMSEventAuthorizationCallback.OnAuthorizeEvent to return a result to the server.

Context

[in] object containing a value defined by the server to identify which call to AuthorizeEvent the plug-in is responding to when it calls IWMSEventAuthorizationCallback.OnAuthorizeEvent. 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.

If a plug-in is not able to verify access to a file because the file could not be found, it is recommended that the plug-in throw NS_E_AUTHORIZATION_FILE_NOT_FOUND (0xc00d1590). The server informs the client that the file could not be found (a 201 error) rather than indicating only that access was denied (a 401 error).

Remarks

Before calling this method, the server calls GetAuthorizedEvents to retrieve an array of events that the plug-in must authorize. The server calls AuthorizeEvent when one of these events occurs.

Example Code

The following example illustrates a possible implementation of the AuthorizeEvent method for an access control list (ACL) authorization plug-in.

void IWMSEventAuthorizationPlugin.AuthorizeEvent(ref WMS_EVENT pEvent,
                                                     IWMSContext pUserCtx,
                                             IWMSContext pPresentationCtx,
                                           IWMSCommandContext pCommandCtx,
                                 IWMSEventAuthorizationCallback pCallback,
                                                           object Context)
{
    WMS_ACCESS_CONTROL wmsAccess = WMS_ACCESS_CONTROL.WMS_ACL_DENY_ALL;
    string strUser = "";

    // This variable is used to represent HRESULT error codes.
    int hr = 0; 

    // Switch on the event type.
    switch (pEvent.Type)
    {
        // Read access.
        case WMS_EVENT_TYPE.WMS_EVENT_DESCRIBE:

        // Read access.
        case WMS_EVENT_TYPE.WMS_EVENT_OPEN:

        // Read access.
        case WMS_EVENT_TYPE.WMS_EVENT_GET_PARAMETER:

        // Write access.
        case WMS_EVENT_TYPE.WMS_EVENT_VALIDATE_PUSH_DISTRIBUTION: 

        try
        {
            // Retrieve the user name from the user context.
            pUserCtx.GetStringValue(WMSDefines.WMS_USER_NAME,       
                                        WMSDefines.WMS_USER_NAME_ID,
                                                    out strUser, 0);

            if (strUser != "")
            {
                // Determine whether the user is in the access control
                // list, and what rights the user has.
                // The GetUserAccess funtion is user-defined.
                m_AccessControl.GetUserAccess(strUser, out wmsAccess);

                if (pEvent.Type == WMS_EVENT_TYPE.WMS_EVENT_OPEN || 
                    pEvent.Type == WMS_EVENT_TYPE.WMS_EVENT_DESCRIBE ||
                    pEvent.Type == WMS_EVENT_TYPE.WMS_EVENT_GET_PARAMETER)
                {

                    // Check to see whether read access is permitted.
                    if (WMS_ACCESS_CONTROL.WMS_ACL_DENY_READ == wmsAccess)
                    {
                        // User was denied read access.
                        // Pass the callback method the integer
                        // value of E_ACCESSDENIED.
                        hr = unchecked((int)0x80070005);
                    }
                    else if (WMS_ACCESS_CONTROL.WMS_ACL_ALLOW_READ == 
                                                                wmsAccess)
                    {
                        // User was granted read access.
                        hr = 0; 
                    }
                    else
                    {
                        // User was neither granted nor denied read access.
                        // Pass the callback method the integer
                        // value of E_FAIL.
                        hr = unchecked((int)0x80004005);
                    }
                }
                else
                {
                    // Check whether write access is permitted.
                    if (WMS_ACCESS_CONTROL.WMS_ACL_DENY_WRITE == 
                                                                wmsAccess)
                    {
                        // User was denied write access.
                        // Pass the callback method the integer
                        // value of E_ACCESSDENIED.
                        hr = unchecked((int)0x80070005);
                    }
                    else if (WMS_ACCESS_CONTROL.WMS_ACL_ALLOW_WRITE == 
                                                                wmsAccess)
                    {
                        // User was granted write access.
                        hr = 0;
                    }
                    else
                    {
                        // User was neither granted nor denied read access.
                        // Pass the callback method the integer
                        // value of E_FAIL.
                        hr = unchecked((int)0x80004005);
                    }
                }
            }

            // Null out string containing user name.
            strUser = "";
        }
        catch (Exception)
        {
            // TODO: Handle exceptions. 
        }
        finally
        {
            // Report the results of the authorization
            // challenge back to the server.
            pCallback.OnAuthorizeEvent(hr, Context);
        }
        break;

        default:
        hr = 0;
        break;
    }
}

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