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

The AttachCurrentSecurityContext method of the MSMQMessage object caches an internal or external certificate and the corresponding private key in a security context structure along with the user's SID and additional security information needed to attach the certificate to a message when requesting authentication. The method also stores the handle to this structure in the object.

This method is superseded by MSMQMessage.AttachCurrentSecurityContext2 in Windows® 2000 Service Pack 2 and later.

Sub AttachCurrentSecurityContext


This method has no parameters.

Return Values

This method has no return values.

Error Codes

For information on return codes, see Message Queuing Error and Information Codes.


Use this method when you are sending a large number of messages using the same certificate or when you are impersonating a user. AttachCurrentSecurityContext creates a security context structure based on the data that is in MSMQMessage.SenderCertificate. If SenderCertificate is not set before calling AttachCurrentSecurityContext, then the Message Queuing internal certificate for the user is used for the security context.

When you are sending authenticated messages, this method retrieves the user's SID and the information that the Message Queuing runtime needs to attach a certificate to the messages, caches the information in a security context structure along with the certificate and the corresponding private key, and stores the handle to this structure in the object. The same object can then be used to send multiple messages by resetting the applicable properties after each message is sent. This provides an easier, more efficient way to send a large number of messages that require authentication using the same certificate.

The memory allocated for the security context structure is freed when the object is destroyed.

When using more than one certificate, the sending application must call AttachSecurityContext on a different MSMQMessage object for each certificate that it uses.

For information on See
What it means to authenticate a message: message integrity and sender authentication Message Authentication
The process used to authenticate a messages How Message Queuing Authenticates Messages
Using a security context structure When to Use a Security Context Structure


Windows NT/2000/XP: Included in Windows NT 4.0 SP3 and later.

Windows 95/98/Me: Included in Windows 95 and later.

Header: Declared in Mqoai.h.

Library: Use Mqoa.lib

See Also