Make Password Hash In ASP.NET Using C#

We take SHA1 or MD5 algorithm to work around for converting your password to hash.

Initial chamber

Step 1: Open Visual Studio 2010 and create an empty website. Give it a suitable name [hashpass_demo].

Step 2: In Solution Explorer you will get your empty website, then add a Web Form by going like this –

For Web Form:

hashpass_demo (Your Empty Website) - Right Click, Add New Item, then Web Form. Name it hashpass_demo.aspx.

Design chamber

Step 3: Now make some design for your application by going to hashpass_demo.aspx and try the code like this:

hashpass_demo.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title></title>  
  8.     <style type="text/css">  
  9.         .style1  
  10.         {  
  11.             width: 258px;  
  12.         }  
  13.         .style2  
  14.         {  
  15.             width: 239px;  
  16.         }  
  17.     </style>  
  18. </head>  
  19. <body>  
  20.     <form id="form1" runat="server">  
  21.     <div>  
  22.       
  23.         <table style="width:100%;">  
  24.             <tr>  
  25.                 <td class="style1">  
  26.                     Enter Your Username:</td>  
  27.                 <td class="style2">  
  28.                     <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>  
  29.                 </td>  
  30.                 <td>  
  31.                     <asp:Label ID="Label2" runat="server"></asp:Label>  
  32.                 </td>  
  33.             </tr>  
  34.             <tr>  
  35.                 <td class="style1">  
  36.                     Enter Your Password:   
  37.                 </td>  
  38.                 <td class="style2">  
  39.                     <asp:TextBox ID="TextBox2" runat="server" TextMode="Password"></asp:TextBox>  
  40.                 </td>  
  41.                 <td>  
  42.                     <asp:Label ID="Label1" runat="server"></asp:Label>  
  43.                 </td>  
  44.             </tr>  
  45.             <tr>  
  46.                 <td class="style1">  
  47.                      </td>  
  48.                 <td class="style2">  
  49.                      </td>  
  50.                 <td>  
  51.                      </td>  
  52.             </tr>  
  53.             <tr>  
  54.                 <td class="style1">  
  55.                      </td>  
  56.                 <td class="style2">  
  57.                     <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Submit" />  
  58.                 </td>  
  59.                 <td>  
  60.                      </td>  
  61.             </tr>  
  62.         </table>  
  63.       
  64.     </div>  
  65.     </form>  
  66. </body>  
  67. </html>  
Your Design looks like the following screenshot:

design

Code chamber

Step 4: Now it’s time for server side coding so that our application starts working. Open your hashpass_demo.aspx.cs file and code it like below.

hashpass_demo.aspx.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Web.Security;  
  8.   
  9. public partial class _Default : System.Web.UI.Page  
  10. {  
  11.     protected void Page_Load(object sender, EventArgs e)  
  12.     {  
  13.   
  14.     }  
  15.     protected void Button1_Click(object sender, EventArgs e)  
  16.     {  
  17.         string securepass = FormsAuthentication.HashPasswordForStoringInConfigFile(TextBox2.Text, "MD5");  
  18.         Label2.Text = "Your Username is-" + TextBox1.Text;  
  19.         Label1.Text = "Your HashPassword is-" + securepass;  
  20.         Label2.ForeColor = System.Drawing.Color.ForestGreen;  
  21.         Label1.ForeColor = System.Drawing.Color.ForestGreen;  
  22.     
  23.     }  
  24. }  
Output chamber

enter password

Output

Hope you liked this. Thank you for reading. Have a good day. 

Up Next
    Ebook Download
    View all
    Learn
    View all