다음을 통해 공유


Visual Basic Code Example: Enforcing 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 Sub procedure that creates a queue that enforces authentication. Authentication is enforced by setting the MSMQQueueInfo.Authenticate property of the destination queue to MQ_AUTHENTICATE.

Note

The MSMQQueueInfo.Authenticate property can be set when the queue is created or later by calling MSMQQueueInfo.Update. If this property is set after the queue is created, the new setting only applies to those messages sent after the queue property is changed.

To enforce authentication when creating a queue

  1. Declare the MSMQQueueInfo variable for the queue.

  2. Create a new MSMQQueueInfo object and assign it to the variable.

  3. Set the MSMQQueueInfo.PathName property to the UNC or DNS path name of the queue.

  4. Set optional queue properties. This example sets the following optional properties.

Code Example

The following code example can be run on all versions of Message Queuing.

Sub EnforceAuthentication( _  
                          strPathname As String _  
                          )  
  Dim qinfo As MSMQQueueInfo  
  
  ' Create new MSMQQueueInfo object.  
  Set qinfo = New MSMQQueueInfo  
  
  ' Specify the path name of the queue.  
  qinfo.PathName = strPathname  
  
  ' Set optional queue properties.  
  qinfo.Label = "TestQueue"  
  qinfo.Authenticate = MQ_AUTHENTICATE  
  
  ' Create queue.  
  On Error GoTo ErrorHandler  
  qinfo.Create  
  
  MsgBox "A queue that accepts only authenticated messages was created."  
  Exit Sub  
  
ErrorHandler:  
  MsgBox "Error " + Hex(Err.Number) + " was returned." _  
          + Chr(13) + Err.Description  
End Sub