--Configure the transport security. USE MASTER go --Create a master key in the master database. --DROP MASTER KEY CREATE MASTER KEY ENCRYPTION BY password = 'MasterKeyPassword' Go CREATE CERTIFICATE ctfSourceServerMaster WITH SUBJECT = 'SourceServerAuth' ACTIVE FOR BEGIN_DIALOG = ON BACKUP CERTIFICATE ctfSourceServerMaster TO FILE = 'C:\Certificates\SourceServer.cer'; GO
--Configure the transport security. USE MASTER go --Create a master key in the master database. --DROP MASTER KEY CREATE MASTER KEY ENCRYPTION BY password = 'MasterKeyPassword' Go CREATE CERTIFICATE ctfTargetServerMaster WITH SUBJECT = 'TargetServerAuth' ACTIVE FOR BEGIN_DIALOG = ON; BACKUP CERTIFICATE ctfTargetServerMaster TO FILE = 'c:\certificates\TargetServer.cer';
--Create the login and the user to own a certificate. CREATE LOGIN remcert WITH PASSWORD = 'LoginPassword' GO CREATE USER remcert FOR LOGIN remcert GO CREATE CERTIFICATE ctftTargetServerMaster AUTHORIZATION remcert FROM FILE = 'C:\Certificates\TargetServer.cer' ACTIVE FOR BEGIN_DIALOG = ON GO --Create a new endpoint for SQL Server 2005 Service Broker, --and set the AUTHENTICATION option to use the ctfSourceServerMaster certificate. CREATE ENDPOINT BrokerEndpoint STATE = STARTED AS TCP ( LISTENER_PORT = 4022 ) FOR SERVICE_BROKER (AUTHENTICATION = CERTIFICATE ctfSourceServerMaster) GO --Grant the required permissions to the remcert login. GRANT CONNECT TO remcert GRANT CONNECT ON ENDPOINT::BrokerEndpoint to remcert GO
--Create the login and the user to own a certificate. CREATE LOGIN remcert WITH PASSWORD = 'LoginPassword' GO CREATE USER remcert FOR LOGIN remcert GO CREATE CERTIFICATE ctfSourceServerMaster AUTHORIZATION remcert FROM FILE = 'c:\certificates\SourceServer.cer' ACTIVE FOR BEGIN_DIALOG = ON GO --Create a new endpoint for SQL Server 2005 Service Broker, --and set the AUTHENTICATION option to use the ctfSourceServerMaster certificate. CREATE ENDPOINT BrokerEndpoint STATE = STARTED AS TCP ( LISTENER_PORT = 4022 ) FOR SERVICE_BROKER (AUTHENTICATION = CERTIFICATE ctfTargetServerMaster) GO --Grant the required permissions to the remcert login. GRANT CONNECT TO remcert GRANT CONNECT ON ENDPOINT::BrokerEndpoint to remcert GO
--Create a new database for testing. CREATE DATABASE SourceDB GO USE SourceDB GO --Configure the dialog security. --Create a master key in the SourceDB database. CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'MasterKeyPassword' CREATE CERTIFICATE ctfDlgSourceServer WITH SUBJECT = 'DlgSourceServer'; BACKUP CERTIFICATE ctfDlgSourceServer TO FILE = 'C:\Certificates\DlgSourceServer.cer'; GO
CREATE DATABASE TargetDB GO USE TargetDB GO --Configure the dialog security. --Create a master key in the TargetDB database. CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'MasterKeyPassword' CREATE CERTIFICATE ctfDlgTargetServer WITH SUBJECT = 'DlgTargetServer'; BACKUP CERTIFICATE ctfDlgTargetServer TO FILE = 'c:\certificates\DlgTargetServer.cer';
--Create a user for the remcert login that owns a certificate for the dialog security. CREATE USER remcert for LOGIN remcert GO CREATE CERTIFICATE ctfDlgTargetServer AUTHORIZATION remcert FROM FILE = 'C:\certificates\DlgTargetServer.cer' ACTIVE FOR BEGIN_DIALOG = ON --Create a message type, a contract, a queue, and a service. CREATE MESSAGE TYPE [mymsg] VALIDATION = NONE CREATE CONTRACT [mycon] ([mymsg] SENT BY ANY) CREATE QUEUE [myQueue] CREATE SERVICE [SourceService] ON QUEUE [myQueue]([mycon]) GO --Grant the send permission to the user. GRANT SEND ON SERVICE::[SourceService] TO remcert --Create a remote service binding for the target service. CREATE REMOTE SERVICE BINDING [Certificate_Binding_on_server] TO SERVICE 'TargetService' WITH USER = remcert, ANONYMOUS=Off --Create a route for the target service. CREATE ROUTE [myRoute] WITH SERVICE_NAME = 'TargetService', address = 'TCP://192.168.89.80:4022';
--Create a user for the remcert login that owns a certificate for the dialog security. CREATE USER remcert for LOGIN remcert GO CREATE CERTIFICATE ctfDlgSourceServer AUTHORIZATION remcert FROM FILE = 'C:\certificates\DlgSourceServer.cer' ACTIVE FOR BEGIN_DIALOG = ON --Create a message type, a contract, a queue, and a service. CREATE MESSAGE TYPE [mymsg] VALIDATION = NONE CREATE CONTRACT [mycon] ([mymsg] SENT BY ANY) CREATE QUEUE [myQueue] CREATE SERVICE [TargetService] ON QUEUE [myQueue]([mycon]) GO --Grant the send permission to the user. GRANT SEND ON SERVICE::[TargetService] TO remcert GO --Create a remote service binding for the target service. CREATE REMOTE SERVICE BINDING [Certificate_Binding_on_server] TO SERVICE 'SourceService' WITH USER = remcert, ANONYMOUS=Off --Create a route for the target service. CREATE ROUTE [myRoute] WITH SERVICE_NAME = 'SourceService', address = 'TCP://192.168.89.79:4022';
USE SourceDB SET NOCOUNT ON DECLARE @conversationHandle uniqueidentifier BEGIN TRANSACTION -- Start dialog. BEGIN DIALOG @conversationHandle FROM SERVICE [SourceService] TO SERVICE 'TargetService' ON CONTRACT [mycon] WITH ENCRYPTION = ON, LIFETIME = 60; -- Send message. SEND ON CONVERSATION @conversationHandle MESSAGE TYPE [mymsg] (N'Hi, from '+@@ServerName) --END CONVERSATION @conversationHandle COMMIT /* select * from sys.transmission_queue select * from sys.dm_broker_connections select * from sys.dm_exec_connections select * from sys.dm_broker_queue_monitors select * from sys.routes select * from myQueue alter database SourceDB set enable_broker select * from sys.databases where is_broker_enabled = 1 */