Hashing Passwords-Easier Password Management by creating and storing the salt and the hashed password in a single value.

Include header

 using System.Web.Helpers;

In View:

To take input

<div class="col-sm-4 tb-font">
   @Html.Label("UName", "User Name", new { style = "width:140px; margin-bottom:5px; margin-top:5px; float: left;" })
   @Html.TextBox("UserName", null, new { @class = "form-control text-box" })
 </div>

                                   
 <div class="col-sm-4 tb-font">
   @Html.Label("Pswrd", "Password", new { style = "width:140px; margin-bottom:5px; margin-top:5px; float: left;" })
   @Html.Password("Password", null, new { @class = "form-control password" })
   </div>

In script: to pass data to controller

<script>
function Save() {
        $.ajax
            ({
                url: "@(Url.Action("CreateAccount", "UserManagement"))",
                data: {  username: $("#UserName").val(), password: $("#Password").val() },
                contentType: "application/json; charset=utf-8",
                type: 'GET',
                dataType: "json",
                success: function (result) {
                    alert("Record saved successfully.");
                },
                error: function () {
                    alert("Something went wrong, please try again");
                }

            });
    }
</script>

In Controller: For Storing Password in DB

public void CreateAccount(string username, string password) 
     
var hashedPassword = Crypto.HashPassword(password);    
 StoreInDatabase(username, hashedPassword); 
}

For Retrieval from DB and Validation

public bool ValidateCredentials(string username, string password)
 {  
   var hashedPassword = GetPasswordFromDatabase(username);     
   var doesPasswordMatch =    Crypto.VerifyHashedPassword(hashedPassword, password);     
return doesPasswordMatch; }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Blog at WordPress.com.

Up ↑

Create your website at WordPress.com
Get started
%d bloggers like this: