Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change expired password without "Password Expired dialog box"

I'm logging on my application using Sql Server Database logon account. However, when a user's password is expired, i can only catch the error message using "error:18488" and display a message to user.

When I login using Sql Server Management Studio with an account with expired password, a dialog box requiring me to change password appears.

Is there a way to allow my application to change the expired password with something like T-Sql statement?

like image 917
Nikko Reyes Avatar asked May 24 '13 08:05

Nikko Reyes


People also ask

What if password expired?

1. First of all, if you are getting “Your password has expired and must be changed” error then there is nothing to worry. Just click on the “OK” button and Windows will allow you to change the password then and there. Keep in mind, you will have to enter the old password.


2 Answers

I've found the solution here, I used SqlConnection.ChangePassword().

Here's the code:

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

Hope it helps others too. :)

like image 196
Nikko Reyes Avatar answered Oct 21 '22 14:10

Nikko Reyes


You can change a user login using ALTER LOGIN.

ALTER LOGIN [userName] WITH PASSWORD=N'StrongPassword'

Another option is to use the Stored Procedure sp_password.

sp_password [ [ @old = ] 'old_password' , ]
     { [ @new =] 'new_password' }
     [ , [ @loginame = ] 'login' ]

C# code:

private readonly SqlConnection m_oConn = new SqlConnection("ConnectionString");
private readonly SqlCommand m_oCmd;

m_oCmd = new SqlCommand("sp_password", m_oConn) { CommandType = CommandType.StoredProcedure };

m_oCmd.Parameters.AddWithValue("@old", null);
m_oCmd.Parameters.AddWithValue("@new", "newpassword");
m_oCmd.Parameters.AddWithValue("@loginame", "username");

var da = new SqlDataAdapter { SelectCommand = m_oCmd };

var ds = new DataSet();
da.Fill(ds);
like image 21
Ramesh Durai Avatar answered Oct 21 '22 16:10

Ramesh Durai