다음을 통해 공유


Visual Basic Code Example: Requesting Authentication Using an Internal Certificate

 

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 sends a message that request authentication using an internal certificate.

For information on how Message Queuing authenticates messages, see Message Authentication.

This example sends the message to a destination queue that is opened with send access using an MSMQDestination object (introduced in MSMQ 3.0). For an example of sending a message to a queue that is opened using an MSMQQueueInfo object, see Visual Basic Code Example: Sending Messages to a Destination Queue Using a Destination Object.

To request authentication using an internal certificate

  1. Declare the objects needed to send the message. This procedure declares the following objects:

  2. Generate a direct format name using the computer name and queue name provided by the caller.

  3. Set the MSMQDestination.FormatName property.

  4. Optional. Call MSMQApplication.RegisterCertificate to ensure that the internal certificate is registered.

  5. Set message properties. This procedure sets the MSMQMessage.AuthLevel property to request authentication.

  6. Call MSMQMessage.Send to send a copy of the message to the destination queue referenced by the format name.

Note

When using an MSMQDestination object, Message Queuing implicitly opens the destination queue with send access.

Code Example

The following code example requires MSMQ 3.0.

Private Sub RequestAuthInternal( _  
                                strComputerName As String, _  
                                strQueueName As String, _  
                                lAuthLevel As Long _  
                                )  
  ' Declare variables.  
  Dim strFormatName As String  
  Dim app As MSMQApplication  
  Dim dest As New MSMQDestination  
  Dim msg As New MSMQMessage  
  
  ' Create a direct format name.  
  strFormatName = "DIRECT=OS:" & strComputerName & "\" & strQueueName  
  
  ' Set the format name of MSMQDestination object.  
  On Error GoTo ErrorHandler  
  dest.FormatName = strFormatName  
  
  ' Register the certificate.  
  RegisterCertificate Flags:=MQCERT_REGISTER_IF_NOT_EXIST  
  
  ' Set message properties.  
  msg.Label = "Test Message"  
  msg.AuthLevel = lAuthLevel  
  
  ' Send the message.  
  msg.Send DestinationQueue:=dest  
  
  Exit Sub  
  
ErrorHandler:  
  MsgBox "Error " + Hex(Err.Number) + " was returned." _  
         + Chr(13) + Err.Description  
End Sub