Visual Basic Code Example: Validating Authentication

 

Applies To: Windows 10, Windows 7, Windows 8, Windows 8.1, Windows Server 2008, Windows Server 2008 R2, Windows Server 2012, Windows Server 2012 R2, Windows Server Technical Preview, Windows Vista

This example provides a Private Sub procedure that peeks at the MSMQMessage.ReceivedAuthenticationLevel property of all the messages in a known queue, displaying the type of signature that was used to sign any messages that requested authentication.

For information on how authenticates messages, see Message Authentication.

This example uses the MSMQMessage.ReceivedAuthenticationLevel property (introduced in MSMQ 2.0) to validate authentication and to determine what type of signature was used to sign the message. Receiving applications using earlier versions of Message Queuing must use the MSMQMessage.IsAuthenticated property, which can only validate that authentication was requested.

To validate authentication

  1. Declare the objects needed to read the messages in the queue.

Note

The New keyword is not used in declaring the MSMQMessage and MSMQQueue objects.

  1. Obtain an MSMQQueueInfo object. The following example obtains the MSMQQueueInfoMSMQQueueInfo object by setting MSMQQueueInfo.PathName using the computer name and queue name provided by the caller.

    Because this procedure sets the PathName property of the MSMQQueueInfo object, Message Queuing must obtain the format name of the queue before opening the queue. The format name of a public queue must be retrieved from the directory service, and the format name of a local private queue can be obtained from information stored on the local computer. However, a remote private queue cannot be opened unless the MSMQQueueInfo.FormatName property is set with a direct format name. This procedure can be modified to receive the format name from the caller or to generate a direct format name. The applicable format name can then be used to set the FormatName property. For more information, see Format Names.

  2. Call MSMQQueueInfo.Open to open the queue with receive access. When opening a queue with receive access the application can peek at or retrieve the messages in the queue.

  3. Call MSMQQueue.PeekCurrent to initialize the cursor. This call points the cursor to the first message in the queue.

  4. Using a loop, peek at the MSMQMessage.ReceivedAuthenticationLevel property of each message in the queue. This example displays the label of all messages that requested authentication and indicates the type of signature that was used to sign them.

  5. When there are no messages left, call MSMQQueue.Close to release resources used to open the queue and exit the Sub procedure.

Code Example

The following code example requires MSMQ 2.0 or later.

Private Sub ValidatingAuthentication( _  
                                     strQueueName As String, _  
                                     strComputerName As String _  
                                     )  
  
  'Declare the Message Queuing objects and a string.  
  Dim msg As MSMQMessage  
  Dim q As MSMQQueue  
  Dim qinfo As New MSMQQueueInfo  
  Dim strPathName As String  
  
  ' Create the path name of the destination queue.  
  strPathName = strComputerName & "\" & strQueueName  
  
  ' Set the path name of the MQMQQueueInfo object and refresh  
  ' its other properties.  
  'On Error GoTo ErrorHandler  
  qinfo.PathName = strPathName  
  qinfo.Refresh  
  
  'Open the queue with receive access.  
  Set q = qinfo.Open(Access:=MQ_RECEIVE_ACCESS, _  
                     ShareMode:=MQ_DENY_NONE)  
  
  'Peek at all the messages in the queue using a cursor.  
  Set msg = q.PeekCurrent(ReceiveTimeout:=1000)  
  Do While Not msg Is Nothing  
    'Display the labels and signature types of messages that requested acknowledgment.  
    Select Case msg.ReceivedAuthenticationLevel  
      Case MQMSG_AUTHENTICATED_SIG10  
        MsgBox "Message: " & msg.Label & " was signed with an MSMQ 1.0 signature." _  
                , , "Authentication Requested"  
      Case MQMSG_AUTHENTICATED_SIG20  
        MsgBox "Message: " & msg.Label & " was signed with an MSMQ 2.0 signature." _  
                , , "Authentication Requested"  
      Case MQMSG_AUTHENTICATED_SIG30  
        MsgBox "Message: " & msg.Label & " was signed with an MSMQ 3.0 multiple-destination digital signature." _  
                , , "Authentication Requested"  
      Case MQMSG_AUTHENTICATED_SIGXML  
        MsgBox "Message " & msg.Label & " was signed with an XML digital signature." _  
                  , , "Authentication Requested"  
      Case Else  
    End Select  
    Set msg = q.PeekNext(ReceiveTimeout:=1000)  
  Loop  
  'Close the queue.  
  q.Close  
  Exit Sub  
  
ErrorHandler:  
  MsgBox "Error " + Hex(Err.Number) + " was returned." _  
         + Chr(13) + Err.Description  
  If Not q Is Nothing And q.IsOpen2 Then  
    q.Close  
  EndIf  
End Sub