SqlConnection.ChangePassword 方法

定义

重载

ChangePassword(String, SqlCredential, SecureString)

更改 SqlCredential 对象中指示的用户的 SQL Server 密码。

ChangePassword(String, String)

将连接字符串中指示的用户的 SQL Server 密码更改为提供的新密码。

ChangePassword(String, SqlCredential, SecureString)

更改 SqlCredential 对象中指示的用户的 SQL Server 密码。

public:
 static void ChangePassword(System::String ^ connectionString, Microsoft::Data::SqlClient::SqlCredential ^ credential, System::Security::SecureString ^ newSecurePassword);
public:
 static void ChangePassword(System::String ^ connectionString, Microsoft::Data::SqlClient::SqlCredential ^ credential, System::Security::SecureString ^ newPassword);
public static void ChangePassword (string connectionString, Microsoft.Data.SqlClient.SqlCredential credential, System.Security.SecureString newSecurePassword);
public static void ChangePassword (string connectionString, Microsoft.Data.SqlClient.SqlCredential credential, System.Security.SecureString newPassword);
static member ChangePassword : string * Microsoft.Data.SqlClient.SqlCredential * System.Security.SecureString -> unit
static member ChangePassword : string * Microsoft.Data.SqlClient.SqlCredential * System.Security.SecureString -> unit
Public Shared Sub ChangePassword (connectionString As String, credential As SqlCredential, newSecurePassword As SecureString)
Public Shared Sub ChangePassword (connectionString As String, credential As SqlCredential, newPassword As SecureString)

参数

connectionString
String

包含连接至服务器的足够信息的连接字符串。 连接字符串不应使用以下任何一个连接字符串关键字:Integrated Security = trueUserIdPassword;或 ContextConnection = true

credential
SqlCredential

SqlCredential 对象。

newSecurePasswordnewPassword
SecureString

新密码。 newSecurePassword 必须为只读。 该密码也必须符合服务器上设置的任何密码安全策略(例如:最小长度、特定字符要求)。

例外

连接字符串包含 UserIdPasswordIntegrated Security=true 的任意组合。

- 或 -

newSecurePassword 的长度超过 128 个字符。

- 或 -

newSecurePassword 不是只读的。

- 或 -

newSecurePassword 是一个空字符串。

其中一个参数(connectionStringcredentialnewSecurePassword)为 null。

适用于

ChangePassword(String, String)

将连接字符串中指示的用户的 SQL Server 密码更改为提供的新密码。

public:
 static void ChangePassword(System::String ^ connectionString, System::String ^ newPassword);
public static void ChangePassword (string connectionString, string newPassword);
static member ChangePassword : string * string -> unit
Public Shared Sub ChangePassword (connectionString As String, newPassword As String)

参数

connectionString
String

包含连接至所需服务器的足够信息的连接字符串。 连接字符串必须包含用户 ID 和当前密码。

newPassword
String

要设置的新密码。 此密码必须符合服务器上设置的任何密码安全策略,包括最小长度、特定字符要求等等。

例外

连接字符串包括将使用集成安全性的选项。

Or

newPassword 超过了 128 个字符。

connectionStringnewPassword 参数为 null。

示例

下面是更改密码的简单示例:

class Program {  
   static void Main(string[] args) {  
      Microsoft.Data.SqlClient.SqlConnection.ChangePassword(  
        "Data Source=a_server;Initial Catalog=a_database;UID=user;PWD=old_password",   
       "new_password");  
   }  
}  
Module Module1  
    Sub Main()  
Microsoft.Data.SqlClient.SqlConnection.ChangePassword(  
        "Data Source=a_server;Initial Catalog=a_database;UID=user;PWD=old_password",   
       "new_password")  
    End Sub  
End Module  

以下控制台应用程序演示了更改用户密码时涉及的问题,因为当前密码已过期。

using Microsoft.Data.SqlClient;

class Program
{
    static void Main()
    {
        try
        {
            DemonstrateChangePassword();
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: " + ex.Message);
        }
        Console.WriteLine("Press ENTER to continue...");
        Console.ReadLine();
    }

    private static void DemonstrateChangePassword()
    {
        // Retrieve the connection string. In a production application,
        // this string should not be contained within the source code.
        string connectionString = GetConnectionString();

        using (SqlConnection cnn = new SqlConnection())
        {
            for (int i = 0; i <= 1; i++)
            {
                // Run this loop at most two times. If the first attempt fails, 
                // the code checks the Number property of the SqlException object.
                // If that contains the special values 18487 or 18488, the code 
                // attempts to set the user's password to a new value. 
                // Assuming this succeeds, the second pass through 
                // successfully opens the connection.
                // If not, the exception handler catches the exception.
                try
                {
                    cnn.ConnectionString = connectionString;
                    cnn.Open();
                    // Once this succeeds, just get out of the loop.
                    // No need to try again if the connection is already open.
                    break;
                }
                catch (SqlException ex)
                {
                    if (i == 0 && ((ex.Number == 18487) || (ex.Number == 18488)))
                    {
                        // You must reset the password. 
                        connectionString =
                            ModifyConnectionString(connectionString,
                            GetNewPassword());

                    }
                    else
                        // Bubble all other SqlException occurrences
                        // back up to the caller.
                        throw;
                }
            }
            SqlCommand cmd = new SqlCommand(
                "SELECT ProductID, Name FROM Product", cnn);
            // Use the connection and command here...
        }
    }

    private static string ModifyConnectionString(
        string connectionString, string NewPassword)
    {

        // Use the SqlConnectionStringBuilder class to modify the
        // password portion of the connection string. 
        SqlConnectionStringBuilder builder =
            new SqlConnectionStringBuilder(connectionString);
        builder.Password = NewPassword;
        return builder.ConnectionString;
    }

    private static string GetNewPassword()
    {
        // In a real application, you might display a modal
        // dialog box to retrieve the new password. The concepts
        // are the same as for this simple console application, however.
        Console.Write("Your password must be reset. Enter a new password: ");
        return Console.ReadLine();
    }

    private static string GetConnectionString()
    {
        // For this demonstration, the connection string must
        // contain both user and password information. In your own
        // application, you might want to retrieve this setting
        // from a config file, or from some other source.

        // In a production application, you would want to 
        // display a modal form that could gather user and password
        // information.
        SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(
            "Data Source=(local);Initial Catalog=AdventureWorks");

        Console.Write("Enter your user id: ");
        builder.UserID = Console.ReadLine();
        Console.Write("Enter your password: ");
        builder.Password = Console.ReadLine();

        return builder.ConnectionString;
    }
}

注解

在 Windows Server 上使用 SQL Server 时,开发人员可以利用允许客户端应用程序同时提供当前密码和新密码的功能来更改现有密码。 如果旧密码已过期,应用程序可以实现诸如在初始登录期间提示用户输入新密码等功能,并且此操作无需管理员干预即可完成。

方法ChangePassword将所提供的connectionString参数中指示的用户SQL Server密码更改为 参数中newPassword提供的值。 如果连接字符串包含集成安全 (选项“Integrated Security=True”或等效) ,则会引发异常。

若要确定密码已过期,调用 Open 方法会 SqlException引发 。 为了指示必须重置连接字符串中包含的密码,Number异常的 属性包含状态值 18487 或 18488。 第一个值 (18487) 指示密码已过期,第二个值 (18488) 指示必须在登录前重置密码。

此方法将打开自己的与服务器的连接,请求密码更改,并在连接完成后立即关闭。 此连接不会从SQL Server连接池中检索,也不会返回到该池。

适用于