如何查看和修改订阅的安全设置(RMO 编程)

复制所需的安全帐户设置(登录名和密码)是在创建发布和订阅时定义的。 您可以稍后通过使用复制管理对象 (RMO) 更改这些设置。 使用的 RMO 类和属性取决于代理的类型和服务器连接的类型。

安全说明安全说明

如果可能,请在运行时提示用户输入安全凭据。 如果必须存储凭据,请使用 Microsoft Windows .NET Framework 提供的 Cryptographic Services(加密服务)。

更改存储在复制服务器上的某个密码的所有实例

  1. 使用 ServerConnection 类创建与复制服务器的连接。

  2. 使用步骤 1 中的连接创建 ReplicationServer 类的实例。

  3. 调用 ChangeReplicationServerPasswords 方法。 指定下列参数:

    • security_mode - 一个用于指定身份验证类型的 ReplicationSecurityMode 值,该类型的密码的所有实例都将进行更改。

    • login - 其密码的所有实例都将进行更改的登录名。

    • password - 新的密码值。

      安全说明安全说明

      如果可能,请在运行时提示用户输入安全凭据。 如果必须存储凭据,请使用 Windows .NET Framework 提供的 Cryptographic Services(加密服务)。

      注意注意

      只有 sysadmin 固定服务器角色的成员才能调用此方法。

  4. 对复制拓扑中需要更新密码的每个服务器重复步骤 1-3。

为事务发布的推送订阅的分发代理更改安全设置

  1. 使用 ServerConnection 类创建与发布服务器的连接。

  2. 创建 TransSubscription 类的实例。

  3. 设置订阅的 PublicationNameDatabaseNameSubscriberNameSubscriptionDBName 属性,并为 ConnectionContext 属性设置步骤 1 中的连接。

  4. 调用 LoadProperties 方法获取该对象的属性。 如果此方法返回 false,则说明步骤 3 中的订阅属性没有正确定义或该订阅不存在。

  5. TransSubscription 实例设置下列一个或多个安全属性:

  6. (可选)如果已将 CachePropertyChanges 的值指定为 true,则调用 CommitPropertyChanges 方法以在服务器上提交更改。 如果将 CachePropertyChanges 的值指定为 false(默认值),则会将更改立即发送到服务器。

为事务发布的请求订阅的分发代理更改安全设置

  1. 使用 ServerConnection 类创建与订阅服务器的连接。

  2. 创建 TransPullSubscription 类的实例。

  3. 设置订阅的 PublicationNameDatabaseNamePublisherNamePublicationDBName 属性,并为 ConnectionContext 属性设置步骤 1 中的连接。

  4. 调用 LoadProperties 方法获取该对象的属性。 如果此方法返回 false,则说明步骤 3 中的订阅属性没有正确定义或该订阅不存在。

  5. TransPullSubscription 实例设置下列一个或多个安全属性:

  6. (可选)如果将 CachePropertyChanges 的值指定为 true,则将调用 CommitPropertyChanges 方法以提交服务器上的更改。 如果将 CachePropertyChanges 的值指定为 false(默认值),则会将更改立即发送到服务器。

为合并发布的请求订阅的合并代理更改安全设置

  1. 使用 ServerConnection 类创建与订阅服务器的连接。

  2. 创建 MergePullSubscription 类的实例。

  3. 设置订阅的 PublicationNameDatabaseNamePublisherNamePublicationDBName 属性,并为 ConnectionContext 属性设置步骤 1 中的连接。

  4. 调用 LoadProperties 方法获取该对象的属性。 如果此方法返回 false,则说明步骤 3 中的订阅属性没有正确定义或该订阅不存在。

  5. MergePullSubscription 实例设置下列一个或多个安全属性:

  6. (可选)如果将 CachePropertyChanges 的值指定为 true,则将调用 CommitPropertyChanges 方法以提交服务器上的更改。 如果将 CachePropertyChanges 的值指定为 false(默认值),则会将更改立即发送到服务器。

为合并发布的推送订阅的合并代理更改安全设置

  1. 使用 ServerConnection 类创建与发布服务器的连接。

  2. 创建 MergeSubscription 类的实例。

  3. 设置订阅的 PublicationNameDatabaseNameSubscriberNameSubscriptionDBName 属性,并为 ConnectionContext 属性设置步骤 1 中的连接。

  4. 调用 LoadProperties 方法获取该对象的属性。 如果此方法返回 false,则说明步骤 3 中的订阅属性没有正确定义或该订阅不存在。

  5. MergeSubscription 实例设置下列一个或多个安全属性:

  6. (可选)如果将 CachePropertyChanges 的值指定为 true,则将调用 CommitPropertyChanges 方法以提交服务器上的更改。 如果将 CachePropertyChanges 的值指定为 false(默认值),则会将更改立即发送到服务器。

更改即时更新订阅服务器连接到事务发布服务器时使用的登录信息

  1. 使用 ServerConnection 类创建与订阅服务器的连接。

  2. 为订阅数据库创建 ReplicationDatabase 类的实例。 为 ConnectionContext 指定步骤 1 中的 NameServerConnection

  3. 调用 LoadProperties 方法获取该对象的属性。 如果此方法返回 false,则说明步骤 2 中的数据库属性定义不正确或此订阅数据库不存在。

  4. 调用 LinkPublicationForUpdateableSubscription 方法,传递以下参数:

    • Publisher - 发布服务器的名称。

    • PublisherDB - 发布数据库的名称。

    • Publication - 即时更新订阅服务器所订阅的发布的名称。

    • Distributor - 分发服务器的名称。

    • PublisherSecurity - PublisherConnectionSecurityContext 对象,用于指定即时更新订阅服务器在连接到发布服务器时使用的安全模式类型和连接的登录凭据。

示例

本示例将检查提供的登录值并为所提供的 Windows 登录名或 SQL Server 登录名(由服务器上的复制存储)更改所有密码。

           // Set the Distributor and distribution database names.
            string serverName = publisherInstance;

            ReplicationServer server;
            
            // Create a connection to the Distributor using Windows Authentication.
            ServerConnection conn = new ServerConnection(serverName);

            try
            {
                // Open the connection. 
                conn.Connect();

                server = new ReplicationServer(conn);

                // Load server properties, if it exists.
                if (server.LoadProperties())
                {
                    string[] slash = new string[1];
                    slash[1] = @"\";

                    // If the login is in the form string\string, assume we are 
                    // changing the password for a Windows login.
                    if (login.Split(slash, StringSplitOptions.None).Length == 2)
                    {
                        //Change the password for the all connections that use
                        // the Windows login. 
                        server.ChangeReplicationServerPasswords(
                                ReplicationSecurityMode.Integrated, login, password);
                    }
                    else
                    {
                        // Change the password for the all connections that use
                        // the SQL Server login. 
                        server.ChangeReplicationServerPasswords(
                                ReplicationSecurityMode.SqlStandard, login, password);
                    }
                }
                else
                {
                    throw new ApplicationException(String.Format(
                        "Properties for {0} could not be retrieved.", publisherInstance));
                }
            }
            catch (Exception ex)
            {
                // Implement the appropriate error handling here. 
                throw new ApplicationException(String.Format(
                    "An error occured when changing agent login " +
                    " credentials on {0}.",serverName), ex);
            }
            finally
            {
                conn.Disconnect();
            }
' Set the Distributor and distribution database names.
Dim serverName As String = publisherInstance

Dim server As ReplicationServer

' Create a connection to the Distributor using Windows Authentication.
Dim conn As ServerConnection = New ServerConnection(serverName)

Try
    ' Open the connection. 
    conn.Connect()

    server = New ReplicationServer(conn)

    ' Load server properties, if it exists.
    If server.LoadProperties() Then

        ' If the login is in the form string\string, assume we are 
        ' changing the password for a Windows login.
        If login.Split("\").Length = 2 Then

            ' Change the password for the all connections that use
            ' the Windows login. 
            server.ChangeReplicationServerPasswords( _
            ReplicationSecurityMode.Integrated, login, password)
        Else

            ' Change the password for the all connections that use
            ' the SQL Server login. 
            server.ChangeReplicationServerPasswords( _
            ReplicationSecurityMode.SqlStandard, login, password)
        End If
    Else
        Throw New ApplicationException(String.Format( _
         "Properties for {0} could not be retrieved.", publisherInstance))
    End If
Catch ex As Exception
    ' Implement the appropriate error handling here. 
    Throw New ApplicationException(String.Format( _
     "An error occured when changing agent login " + _
     " credentials on {0}.", serverName), ex)
Finally
    conn.Disconnect()
End Try