Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Membership API force password change

Tags:

asp.net

I am using ASP.NET Membership API. I want to force the user to change their password after the first time he logs in. But, I could not find any such built in functionality in the Membership API of ASP.NET.
Is it possible, or not? If yes, how can it be done easily?

like image 282
Imran Jawaid Avatar asked Mar 19 '12 12:03

Imran Jawaid


1 Answers

Here you are, a fully tested solution ;)

protected void LoginButton_Click(object sender, EventArgs e)
{
    /****note: UserName and Password are textbox fields****/

    if (Membership.ValidateUser(UserName.Text, Password.Text))
    {
        MembershipUser user = Membership.GetUser(UserName.Text);
        if (user == null)
        {
           FailureText.Text = "Invalid username. Please try again.";
           return;
        }
        if (user.IsLockedOut)
           user.UnlockUser();

        /* this is the interesting part for you */
        if (user.LastPasswordChangedDate == user.CreationDate) //if true, that means user never changed their password before
        {
            //TODO: add your change password logic here
        }
    }
}

In case you need help in how to change password, please let me know.

Should this post be in any help for you, please tag as answer

like image 148
m.othman Avatar answered Sep 20 '22 11:09

m.othman