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