Tuesday, 21 November 2023

Learn How to Change Password in ASP.NET Using C#

In the ever-evolving landscape of web development, security remains a top priority. One crucial aspect of safeguarding user accounts is enabling them to change their passwords securely at any point of time. If you're working with ASP.NET using C#, you're in the right place to learn the ropes of password management. In this comprehensive guide, we'll walk you through the process of changing passwords in ASP.NET web form using C#, ensuring your web applications maintain the highest level of security.

Why Changing Passwords Matters

Before diving into the technical details, let's briefly discuss why allowing users to change their passwords is so important. Regularly updating passwords is a fundamental security practice that helps protect user accounts from unauthorized access. By implementing a seamless password change process, you enable users to take control of their account security and stay one step ahead of potential threats.

Learn How to Change Password in ASP.NET Using C#

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="password.aspx.cs" Inherits="CRUD.password" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
     <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
    <title>Login</title>
</head>
<body>
    <form id="form1" runat="server">
        <div class="container">
            <div class="row">
                <div class="col-md-12">
                    <h1 class="text-center">Change your password!</h1>
                </div>
                <div class="row">
                    <div class="col-md-12">
                        <h3 class="text-white text-center">Welcome: &nbsp; <span class="bg-success text-white text-center">
                            <asp:Label ID="lblUser" runat="server" Text="lblUser"></asp:Label></span></h3>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-2">
                        
                    </div>
                    <div class="col-md-8">
                          
                        <label for="currentpassword" class="form-label">Current Password</label>
                        <asp:TextBox ID="Password" runat="server"  class="form-control" TextMode="Password"></asp:TextBox>

                        <label for="newpassword" class="form-label">New Password</label>
                        <asp:TextBox ID="newPassword" runat="server"  class="form-control" TextMode="Password"></asp:TextBox>

                        <label for="reTypepassword" class="form-label">retype Password</label>
                        <asp:TextBox ID="reTypepassword" runat="server"  class="form-control" TextMode="Password"></asp:TextBox>

                       

                        <asp:Button ID="BtnChange" runat="server" Text="Change" CssClass="form-control btn btn-primary mt-3" OnClick="BtnChange_Click" />
                        
                        
                    </div>
                    <div class="col-md-2"></div>
                </div>
            </div>
            <div class="row">
                    <div class="col-md-12">
                        
                            <asp:Label ID="lblMessage" runat="server" Text="LblMessage" CssClass="bg-success text-white text-center"></asp:Label>
                    </div>
                </div>
        </div>
    </form>
     <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js" />
</body>
</html>

Implementing Password Change Functionality

Now , let's dive into the process of changing the password of a user after logging 
in the system.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace CRUD
{
    public partial class password : System.Web.UI.Page
    {
        string connectionString;
        SqlConnection conn;
        protected void Page_Load(object sender, EventArgs e)
        {
            connectionString = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=website";
            conn = new SqlConnection(connectionString);
            if(Session["Username"] != null)
            {
                lblUser.Text = Session["Username"].ToString();
            }            
        }

        protected void BtnChange_Click(object sender, EventArgs e)
        {
            SqlCommand cmd = new SqlCommand(" Update users set Upassword=@upassword where UName=@uname", conn);
            cmd.Parameters.AddWithValue("@upassword", newPassword.Text);
            cmd.Parameters.AddWithValue("@uname", lblUser.Text);
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
            lblMessage.Text = "Password changed successfully!";

        }
    }
}
By following these steps, you've successfully implemented a secure password change functionality in your ASP.NET project using C#. Regularly updating passwords is a key practice in maintaining a robust security posture. Remember to adapt the code snippets to fit the specifics of your project and always prioritize the use of secure practices, such as hashing passwords and validating user credentials.  >Now, your web application is equipped with a robust mechanism for users to change their passwords securely. Stay vigilant, stay secure!

ASP.NET, CSS, HTML

0 comments:

Post a Comment

 

© 2018 Mastering Web Development: HTML, Bootstrap, PHP, ASP.NET & VB.NET Essentials - Designed by Mukund | Privacy Policy | Sitemap

About Me | Contact Me | Write For Us