SqlConnection.ChangePassword メソッド
アセンブリ: System.Data (system.data.dll 内)

Dim connectionString As String Dim newPassword As String SqlConnection.ChangePassword(connectionString, newPassword)


Windows Server 2003 以降で SQL Server 2005 を使用している場合、現在のパスワードと新しいパスワードの両方を指定して既存のパスワードを変更できるようにする機能をクライアント アプリケーションに実装できます。たとえば、最初のログイン時に、古いパスワードの期限が切れていた場合、管理者が介入することなく、ユーザーに新しいパスワードの入力を求めることもできます。
ChangePassword は、connectionString パラメータに指定されたユーザーの SQL Server パスワードを、newPassword パラメータに指定された値に変更できるようにするメソッドです。接続文字列に統合セキュリティ用のオプション ("Integrated Security=True" など) が含まれていた場合、例外がスローされます。
パスワードの有効期限が切れているかどうかは、Open メソッドを使用し、SqlException を発生させることによって判断できます。接続文字列中のパスワードをリセットする必要がある場合は、Number プロパティに 18487 または 18488 のステータス値が格納されます。ここで、18487 はパスワードの有効期限が切れていることを示し、18488 はパスワードをリセットしてからログインする必要があることを示します。
このメソッドでは、サーバーに対する専用の接続を確立し、パスワードの変更を要求して、変更が完了した段階で接続が解除されます。この接続が SQL Server の接続プールから取得されたり、逆に接続プールに戻されたりすることはありません。

次のコンソール アプリケーションでは、現在のパスワードの有効期限が切れている場合に、ユーザーにパスワードの変更を求めます。
Option Explicit On Option Strict On Imports System Imports System.Data Imports System.Data.SqlClient Module Module1 Sub Main() Try DemonstrateChangePassword() Catch ex As Exception Console.WriteLine("Error: " & ex.Message) End Try Console.WriteLine("Press ENTER to continue...") Console.ReadLine() End Sub Private Sub DemonstrateChangePassword() Dim connectionString As String = GetConnectionString() Using cnn As New SqlConnection() For i As Integer = 0 To 1 ' 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. Exit For Catch ex As SqlException _ When (i = 0 And (ex.Number = 18487 Or ex.Number = 18488)) ' You must reset the password. connectionString = ModifyConnectionString( _ connectionString, GetNewPassword()) Catch ex As SqlException ' Bubble all other SqlException occurrences ' back up to the caller. Throw End Try Next Dim cmd As New SqlCommand("SELECT ProductID, Name FROM Product", cnn) ' Use the connection and command here... End Using End Sub Private Function ModifyConnectionString( _ ByVal connectionString As String, ByVal NewPassword As String) As String ' Use the SqlConnectionStringBuilder class to modify the ' password portion of the connection string. Dim builder As New SqlConnectionStringBuilder(connectionString) builder.Password = NewPassword Return builder.ConnectionString End Function Private Function GetNewPassword() As String ' 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() End Function Private Function GetConnectionString() As String ' 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. Dim builder As 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 End Function End Module
using System; using System.Data; using System.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 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- SqlConnection.ChangePassword メソッドのページへのリンク