My scenrio is encrypt password
for example:
User click on chage password link after that
User enter Username and New Password and then new password is in encrpypt form
My scenrio is encrypt password
for example:
User click on chage password link after that
User enter Username and New Password and then new password is in encrpypt form
public static string EncodePasswordToBase64(string password)
{
try
{
byte[] encData_byte = new byte[password.Length];
encData_byte = System.Text.Encoding.UTF8.GetBytes(password);
string encodedData = Convert.ToBase64String(encData_byte);
return encodedData;
}
catch (Exception ex)
{
throw new Exception("Error in base64Encode" + ex.Message);
}
}
protected void btn_Login_Click(object sender, EventArgs e)
{
DatabaseContext db = new DatabaseContext();
var bckofc = new Models.OfficeUser();
{
try
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Agri"].ToString());
}
catch { }
}
try
{
string uid = txt_Username.Text;
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Agri"].ToString());
con.Open();
string qry = "select Id from officeUsers where Username='" + uid + "'";
SqlCommand cmd = new SqlCommand(qry, con);
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.HasRows)
{
while (sdr.Read())
{
Session["Username"] = uid;
Session["Id"] = sdr["Id"];
}
lblMessage.Text = EncodePasswordToBase64(txt_password.Text);
}
else
{
}
con.Close();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
db.OfficeUsers.Add(bckofc);
db.SaveChanges();
}
Need help for this
Thanks in advance
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Rajkiran SwainPosted May 2, 2023, 10:30 AM
To encrypt a password in ASP.NET, you can use the
System.Security.Cryptographynamespace which provides various encryption and decryption methods. One of the most commonly used methods is hashing.Here's an example of how to hash a password using the SHA256 algorithm:
In this example, we create an instance of the
SHA256class and compute the hash of the UTF8-encoded password. The resulting hash is converted to a lowercase string without any dashes and returned.To use this method, you can modify your
btn_Login_Clickevent handler as follows:Here, we first retrieve the username and password from the text boxes. Then, we use the
HashPasswordmethod to hash the password. Finally, you can compare the username and hashed password against your database to authenticate the user.Rajeesh MenothPosted May 2, 2023, 10:08 AM
Hi,
A simple example for encrypting & decrypt passwords:
https://www.c-sharpcorner.com/blogs/how-to-encrypt-or-decrypt-password-using-asp-net-with-c-sharp1