-- This lives on the main instance
--

USE BrokerDB1
GO

-- 
-- Step 1-Certs: Create a certificate for dialog encryption
-- This will be used to represent your service user in the other databasse
--
create master key encryption by password = 'StrngPW1'

CREATE CERTIFICATE cert1
with subject = 'cert1',
start_date = '09/01/2005'
active for begin_dialog = on;
go

--
-- Back the certificate up to a file
-- This only backs up the public key, not the private key
--
BACKUP CERTIFICATE cert1
TO FILE = 'c:\certs\cert1.cer'
GO

--
-- Step 3-Certs:
-- Create a user in this database (without login or any other priv)
--

CREATE USER user1 WITHOUT LOGIN
GO

--
-- The user owns a certificate created by using
-- the public key only-portion from the partner's database
--

CREATE CERTIFICATE cert2_remote
AUTHORIZATION user1
FROM FILE = 'c:\certs\cert2.cer'
GO

-- Step 5-Certs: CREATE A REMOTE SERVICE BINDING (1) or (2)

-- This one (1)
-- With this one, the other side needs to give
-- the user that corresponds to the cert
-- SEND privilege on the SERVICE
CREATE REMOTE SERVICE BINDING [RemoteInventory]
TO SERVICE 'InvService' 
WITH  USER = user1,
ANONYMOUS=OFF
GO

-- Or this one (2)
-- With this one, the other side needs to give
-- SEND privilege on the SERVICE TO PUBLIC
CREATE REMOTE SERVICE BINDING [RemoteInventoryAnon1]
TO SERVICE 'InvService' 
WITH  USER = user1,
ANONYMOUS=ON
GO

-- Diagnostic queries

DROP REMOTE SERVICE BINDING RemoteServerAnon1
GO

-- You can only have 1 remote service binding per service
select * from sys.remote_service_bindings