如何:設定完整對話安全性的目標服務 (Transact-SQL)

適用於:SQL ServerAzure SQL 受控執行個體

SQL Server 在和服務交談時,都會使用對話方塊安全性,期間裝載起始服務的資料庫中,會有一個遠端服務繫結。 當裝載目標服務的資料庫包含與建立對話之使用者對應的使用者時,對話會使用完整安全性。

若要確保目標服務使用對話安全性,請建立要以之登入的起始服務使用者。 對於每個起始服務,建立一個使用者並為起始使用者安裝憑證。 請注意,目標服務不使用遠端服務繫結。

若要設定完整對話安全性的目標服務

  1. 建立不含登入的使用者。

  2. 為使用者建立憑證。

    注意

    必須使用主要金鑰加密憑證。 如需詳細資訊,請參閱 CREATE MASTER KEY (Transact-SQL)

  3. 讓該使用者成為目標服務的擁有者。

  4. 將憑證備份至檔案。

    注意

    僅備份此使用者的憑證。 不要備份或散發與憑證相關聯的私密金鑰。

  5. 授與目標服務使用者從目標服務所使用的佇列接收訊息的權限。

  6. 向遠端資料庫的資料庫管理員提供起始服務的憑證和名稱。

    注意

    SQL Server 如需使用完整對話安全性,憑證必須安裝在遠端資料庫中,並且憑證的使用者必須是在目標服務的遠端服務繫結中指定的使用者。

  7. 從受信任的來源取得遠端資料庫中使用者的憑證。 通常,這涉及使用加密的電子郵件傳送憑證,或在實體媒體 (如磁片) 上傳送憑證。

    注意

    僅安裝來自受信任來源的憑證。

  8. 建立不含登入的使用者。

  9. 安裝起始服務的憑證。 前一步驟中建立的使用者會擁有憑證。

  10. 為起始服務憑證建立不含登入的使用者。

  11. 授與起始使用者將訊息傳送至目標服務的權限。

範例

注意

這些範例已經過 SQL Server 2008 R2 (10.50.x) 的驗證。 建議使用 AdventureWorks 範例資料庫中的 AdventureWorks2008R2 範例資料庫來完成這些範例。

最新版的 SQL Server 支援 SQL Server Service Broker。

    USE AdventureWorks2008R2 ;
    GO

    --------------------------------------------------------------------
    -- The first part of the script configures security for the local user.
    -- The script creates a user in this database, creates a certificate
    -- for the user, writes the certificate to the file system, and
    -- grants permissions to the user. Since this service is a target
    -- service, no remote service binding is necessary.


    -- Create a user without a login. For convenience,
    -- the name of the user is based on the name of the
    -- the remote service.

    CREATE USER [SupplierOrdersUser]
        WITHOUT LOGIN;
    GO

    -- Create a certificate for the initiating service
    -- to use to send messages to the target service.

    CREATE CERTIFICATE [SupplierOrdersCertificate]
        AUTHORIZATION [SupplierOrdersUser]
        WITH SUBJECT = 'Certificate for the SupplierOrders service user.';
    GO

    -- Dump the certificate. Provide the certificate file
    -- to the administrator for the database that hosts
    -- the other service.

    BACKUP CERTIFICATE [SupplierOrdersCertificate]
       TO FILE = 'C:\Certificates\SupplierOrders.cer';
    GO
    -- Make this user the owner of the target service.

    ALTER AUTHORIZATION ON SERVICE::SupplierOrders TO [SupplierOrdersUser];
    GO

    -- Grant receive on the orders queue to the local user.

    GRANT RECEIVE ON SupplierOrdersQueue
        TO [SupplierOrdersUser];
    GO

    ---------------------------------------------------------------
    -- The second part of the script configures security in this
    -- database for the remote service. This consists of creating
    -- a user in this database, loading the certificate for the remote
    -- service, and granting permissions for the user.


    -- Create a user without a login.

    CREATE USER [OrderPartsUser]
        WITHOUT LOGIN;
    GO

    -- Install a certificate for the initiating user.
    -- The certificate is provided by the owner of the
    -- initiating service.

    CREATE CERTIFICATE [OrderPartsCertificate]
        AUTHORIZATION [OrderPartsUser]
        FROM FILE='C:\Certificates\OrderParts.cer';
    GO

    -- Grant send on the target service to the user for the
    -- initating service.

    GRANT SEND ON SERVICE::[SupplierOrders]
        TO [OrderPartsUser];
    GO

另請參閱