更改密码的C#代码? [英] C# code for change password?

查看:73
本文介绍了更改密码的C#代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





任何人都知道这个?





C#更改密码的代码?



i有一个设置页面...在该页面我应该更改密码..

代码:



Anyone know this?


C# code for change password?

i have a settings page...in that page i should do the password changes..
code:

protected void btnchange_Click(object sender, EventArgs e)
  {
      int changed;
      string loginemailid = txtloginemailid.Text.Trim();
      string oldpass = txtoldpassword.Text.Trim();
      string newpass = txtnewpassword.Text.Trim();


      if (loginemailid != null && oldpass != null)
      {
          changed = mydac.changeuserpassword(loginemailid, oldpass, newpass);
          if (changed == 1)
          {
              lblmsg.Text = "Your new Password Updated";

          }
      }
      else
      {
          lblmsg.Text = "Updation Failed,Ensure Your Login Information Is Correct";
      }
  }

推荐答案

假设您有2个名为txtNewPassword和txtConfirmedPassword的文本框。

在输入这两个文本框后,在保存按钮上单击事件执行以下操作:

1.获取数据表中当前用户的记录。

2.如果记录计数> 0然后触发更新查询以更新当前用户的新密码。



示例代码:

Suppose you have 2 textbox named txtNewPassword and txtConfirmedPassword.
After feeding these two textboxes, On the Save Button Click Event Do The Following:
1. Fetch The Record of current user in the datatable.
2. If recordcount > 0 then fire an update query to update the current user's new password.

Sample Code:
if (txtNewPassword.Text == txtConfirmPassword.Text)
    {
        mSQL = "SELECT * FROM TableName WHERE user_name = '" + txtUserName.Text + "' AND password = '" + txtPassword.Text + "'";
        mDT_Save = mDBHelper.GetTable(mSQL);
        if (mDT_Save.Rows.Count > 0)
        {
            for (int i = 0; i < mDT_Save.Rows.Count; i++)
            {
                mSQL = "UPDATE TableName SET password = '" + txtConfirmPassword.Text + "' WHERE user_name = '" + Convert.ToString(mDT_Save.Rows[i]["user_name"]) + "'";
                mDBHelper.ExecuteSQLNonQuery(mSQL);
            }
            MessageBox.Show("Password Changed Successfully");
            txtPassword.Text = txtConfirmPassword.Text;
            return;
       }
       else
       {
            MessageBox.Show("User Not Found");
            return;
       }
    }





希望通过这个示例代码,你会得到一个想法



Hope through this sample code you will get an idea


您可以通过各种方法使用

1 => 更改密码控制

2 =>您可以使用三个文本框。

输入:旧密码

输入:新

输入:确认(匹配新的和确认文本框文本)

3 =>

change-user-password-in-asp-net-forms-authentication



简单的方式



as

You can use it by various methods
1 =>Change Password Control
2=>You can take three text boxes.
input:Old password
Input:New
Input:Confirm(Match both new and confirm textbox text)
3=>
change-user-password-in-asp-net-forms-authentication

Simple way

as
protected void Button1_Click(object sender, EventArgs e)
{

string paswd;
paswd="NewPass" // set your password
String str = "update reg set password=@pass where u_name=@u_name and password=@oldpass ";
cmd = new SqlCommand(str, con);
cmd.Parameters.AddWithValue("oldpass",oldpass.Text);
cmd.Parameters.AddWithValue("pass",paswd);
cmd.Parameters.AddWithValue("u_name",txt_nPW.Text);

con.Open();
cmd.ExecuteNonQuery();
con.Close();
Response.Write("Password Changed");

}


class Userdetails
	{
            public static string username;
            public static string password;
     }

SqlConnection Connection = new SqlConnection("Data Source=FARZADFWADIA-PC;Initial Catalog=HRMS;Integrated Security=True");
Connection.Open();

if (textBox_New.Text.Length < 4)
	{
		MessageBox.Show("The Length of the password must be 			atleast in four chars.", "Update Failed"); return;
	}

if (textBox_New.Text != textBox_Verify.Text)
	{
		MessageBox.Show("New Password dose not match Confirm 			Password.", "Update Failed"); return;
	}

if (Userdetails.password != textBox_Current.Text)
	{
		MessageBox.Show("Old Password is not valid.", "Update 		Failed");
		return;
	}

Userdetails.password = textBox_New.Text;
SqlCommand cmd = new SqlCommand("update Login set password=@UserPassword where username="+Userdetails.username);
cmd.Parameters.AddWithValue("@UserPassword", textBox_New.Text);

if (cmd.ExecuteNonQuery().ToString() == "0")
	{
		MessageBox.Show("Password changed successfully.", 			"Process Completed", MessageBoxButtons.OK, 				MessageBoxIcon.Information);
	}
else
	{
		MessageBox.Show("Due to some problems your password 			cannot be changed. Contact your administrator to 			change it 	or try again later.", "Update Failed", 			MessageBoxButtons.OK,MessageBoxIcon.Warning);
	}

this.Close();


这篇关于更改密码的C#代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆