本文將告訴您,如何使用 SQL Server 2005 的多個執行個體 (Instance) 上的 Microsoft SQL Server 2005 服務中繼程式遠端安全性憑證。在這種情況下使用憑證,您可以提供安全連線。
SQL Server 2005 服務中繼程式遠端安全性包括牽涉到多個 SQL Server 執行個體,當那些作業使用任一個對話安全性或傳輸安全性的作業。下列範例示範如何使用 SQL Server 2005 的兩個執行個體 (Instance) 上的 SQL Server 2005 服務中繼程式遠端安全性憑證。本範例假設下列情況皆成立:
- 使用 [憑證建立工具 (Makecert.exe) 來建立四個憑證和四個私用金鑰檔案。本範例假設這些檔案已經複製到兩台伺服器上的 [C:\Certificates] 資料夾,而且該檔案的命名下列:
- SourceServer.cer
- SourceServer.pvk
- TargetServer.cer
- TargetServer.pvk
- DlgSourceServer.cer
- DlgSourceServer.pvk
- DlgTargetServer.cer
- DlgTargetServer.pvk
如需有關如何建立供測試憑證的詳細資訊,請造訪下列 Microsoft 開發 o 人 h 員 ? 工 u 具 ? 網路 (MSDN) 網站]: - 您在相同的網路中的不同伺服器上安裝兩個 SQL Server 2005 執行個體。本範例假設第一部伺服器會為 ServerSrc,第二個伺服器為 ServerTag。
- 您使用 SQL Server sysadmin 固定伺服器角色的成員的登入連線至兩個執行個體。
- 您所做確定 TCP 連接埠 4022 可以使用。在這個範例連接埠將用於由兩個執行個體相互連接。
所有先前的條件都符合時,請使用下列程序。
為 SQL Server 執行個體 ServerSrc 伺服器上設定 SQL Server 2005 服務中繼程式
- 藉由使用 SQL Server 管理 Studio,以連接到 ServerSrc 伺服器上執行個體。
- 查詢編輯器中執行下列的 Transact-SQL 陳述式:
--Configure the transport security.
USE MASTER
go
--Create a master key in the master database.
CREATE MASTER KEY ENCRYPTION BY password = 'MasterKeyPassword'
Go
--Create a certificate for transport security.
CREATE CERTIFICATE ctfSourceServerMaster
FROM FILE = 'C:\Certificates\SourceServer.cer'
WITH PRIVATE KEY ( FILE = 'C:\Certificates\SourceServer.pvk' , DECRYPTION BY PASSWORD = 'PrivateKeyPassword' )
ACTIVE FOR BEGIN_DIALOG = ON
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 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 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 a certificate for the SourceDB database.
CREATE CERTIFICATE ctfDlgSourceServer
FROM FILE = 'C:\certificates\DlgSourceServer.cer'
WITH PRIVATE KEY
(FILE='C:\certificates\DlgSourceServer.pvk',decryption by password='PrivateKeyPassword')
ACTIVE FOR BEGIN_DIALOG = ON
GO
--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://ServerTag:4022';
筆記MasterKeyPassword 是您必須指定資料庫的主要金鑰密碼的預留位置。 PrivateKeyPassword 是私用金鑰您指定的.pvk 私用金鑰檔的密碼預留位置使用憑證建立工具。LoginPassword 是新建立的登入密碼預留位置。
為 SQL Server 執行個體 ServerTag 伺服器上設定 SQL Server 2005 服務中繼程式
- 藉由使用 SQL Server 管理 Studio,以連接到 ServerTag 伺服器上執行個體。
- 查詢編輯器中執行下列的 Transact-SQL 陳述式:
--Configure the transport security.
USE MASTER
go
--Create a master key in the master database.
CREATE MASTER KEY ENCRYPTION BY password = 'MasterKeyPassword'
Go
--Create a certificate for transport security.
CREATE CERTIFICATE ctfTargetServerMaster
FROM FILE = 'c:\certificates\TargetServer.cer'
WITH PRIVATE KEY (FILE='c:\certificates\TargetServer.pvk',decryption by password='PrivateKeyPassword')
ACTIVE FOR BEGIN_DIALOG = ON
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 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 a certificate for the TargetDB database.
CREATE CERTIFICATE ctfDlgTargetServer
FROM FILE = 'c:\certificates\DlgTargetServer.cer'
WITH PRIVATE KEY
(FILE='c:\certificates\DlgTargetServer.pvk',decryption by password='PrivateKeyPassword')
ACTIVE FOR BEGIN_DIALOG = ON
GO
--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://ServerSrc:4022';
測試遠端安全性的 SQL Server 2005 服務中繼程式
設定兩個執行個體之後連接到 ServerSrc] 伺服器上執行個體,然後執行下列陳述式,若要測試 SQL Server 2005 服務中繼程式服務:
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 = 600;
-- Send message.
SEND ON CONVERSATION @conversationHandle
MESSAGE TYPE [mymsg] (N'Hi, from '+@@ServerName)
COMMIT
執行這些陳述式之後連接到 ServerTag] 伺服器上執行個體,然後執行下列陳述式:
SELECT CONVERT(NVARCHAR(MAX),message_body) FROM myQueue
GO
您會收到下列結果:
如需有關 SQL Server 2005 服務中繼程式的詳細資訊,請參閱下列主題 SQL Server 2005 線上叢書 》 中的]:
- 管理安全性 (服務仲介)
- 網路和遠端安全性
- 服務中繼程式對話安全性
- 判斷對話安全性類型
- 遠端服務繫結