Garima Bansal

Garima Bansal

  • 1.1k
  • 697
  • 41.6k

encrypt password in asp.net

May 2 2023 9:43 AM

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

<div class="row justify-content-around h-100 d-flex align-items-center">
        <div class="col-md-5  col-sm-12 border-1 border">
            <div class="row bg-secondary text-light" style="">

                <label class="p-2"><strong>Change Password</strong> </label>
            </div>
            <div class="row">
                <asp:ValidationSummary runat="server" CssClass="text-danger" />
            </div>

             <div class="row form-group">
                <label for="txt_Username" class="form-label mb-0 mt-2">
                    Username
                        <asp:RequiredFieldValidator runat="server" Text="*" CssClass="text-danger" ControlToValidate="txt_Username" ErrorMessage="Username is required" />
                </label>
                <asp:TextBox runat="server" ID="txt_Username" placeholder="Enter Username" CssClass="form-control"></asp:TextBox>
            </div>
            <div class="row form-group">
                <label for="txt_password" class="form-label mb-0 mt-2">
                    Password
                        <asp:RequiredFieldValidator runat="server" Text="*" CssClass="text-danger" ControlToValidate="txt_password" ErrorMessage="Password is required" />
                </label>
                <asp:TextBox runat="server" ID="txt_password" TextMode="Password" placeholder="Enter Password" CssClass="form-control"></asp:TextBox>
            </div>

            <div class="row form-group">
                <label for="txt_password" class="form-label mb-0 mt-2">
                    Confirm  Password
                    <asp:CompareValidator runat="server" ControlToCompare="txt_password" ControlToValidate="changepassword"
                        ErrorMessage="Passwords do not match." ForeColor="Red" Display="Dynamic">
                    </asp:CompareValidator>
                </label>
                <asp:TextBox runat="server" ID="changepassword" TextMode="Password" placeholder="Enter Confirm Password" CssClass="form-control"></asp:TextBox>
            </div>

            <div class="row form-group my-2">
                <asp:Label runat="server" ID="lblMessage" CssClass="text-danger" Visible="false" /><br />
                <asp:Button runat="server" ID="btn_Login" CssClass="btn btn-lg text-light" Text="Submit" BackColor="#009970" OnClick="btn_Login_Click" />
            </div>
        </div>
    </div>
</div>
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


Answers (2)