• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 535
  • Last Modified:

need linq code snippet to change password


I need a linq to sql code snippet that changes a user password.  I am not using membership provider

The arguments are

ChangePassword(User.Identity.Name, currentPassword, newPassword))


So check for valid user and password so if it is correct change to new password
0
ToString1
Asked:
ToString1
  • 2
1 Solution
 
Fernando SotoCommented:
Hi ToString1;

It would help to give the Database name, table names and columns names that are to be used and if ChangePassword is a call to a Stored Procedure or a function in your program.

Fernando

0
 
ToString1Author Commented:
Ok

Table called USER

UserId.  Int
Username.  Varchar(100)
Password.  Nvarchar(255)
Email. Nvarchar(255)

0
 
Fernando SotoCommented:
Hi ToString1;

This should do what you want.

Fernando
// User to look up and change password
string userName = "User Name As String";
string currentPassword = "User Password";
// New password to replace old one.
string changePassword = "New Password";

// Create data context
DataClasses1DataContext usersDb = new DataClasses1DataContext();

// Get the user whos password is to be changed
USER user = (from u in usersDb.USER
             where u.Username == userName && u.Password == currentPassword
             select u).FirstOrDefault();

// Check to see if user is in the DB or not
if (user != null)
{
    // User was found, change the password
    user.Password = changePassword;
    // Save the new password to the DB
    usersDb.SubmitChanges();
}
else
{
    // Username, password or both are incorrect
    Response.Write("Invalid Username, Password or both.");
}

Open in new window

0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now