I am trying to get a login form to work using the below method. I am using JQuery version 1.12.0 and MVC5. i just create a accounts controller, when the admin login in, he/she can do CRUD functionality, others users can just see the posts and comments and nothings, but the login page not working means when I press Login nothing happens. The page does not refresh. It behaves as is I am clicking nothing this is login view
@model string
@{
ViewBag.Title = "Login";
}
@section ExtraHeaders
{
<script src="@Url.Content("~/Scripts/Login.js")" type="text/javascript</script>
<script src="@Url.Content("~/Scripts/SHA256.js")" type="text/javascript</script>
}
<form action="@Href("~/Accounts/Login")" method="post" id="loginForm">
<input type="text" name="name" id="name"/> Name <br />
<input type="password" name="password" id="password"/> Password <br />
<input type="hidden" name="nonce" id="nonce" value="@Model"/>
<input type="hidden" name="hash" id="hash" value="hash"/>
<input type="button" onclick="getPasswordHash('password', 'nonce','hash'); $('#loginForm').submit();" value="Login"/>
</form>
this is login js file code
function getPasswordHash (passwordElement, nonceElement, hashElement)
{
var password = $('#' + passwordElement).attr('value');
var nonce = $('#' + nonceElement).attr('value');
$('#' + hashElement).attr('value', $.sha256(password + nonce));
$('#' + passwordElement).attr('value', '');
}
this is account controller for login
private BlogModel model= new BlogModel();
public ActionResult Login(string name, string hash)
{
if(string.IsNullOrWhiteSpace(hash))
{
Random random = new Random();
byte[] randomData = new byte[sizeof(long)];
random.NextBytes(randomData);
string newNonce = BitConverter.ToInt64(randomData, 0).ToString("X16");
Session["Nonce"] = newNonce;
return View(model: newNonce);
}
Administrator admin = model.Administrators.Where(x => x.Name == name).FirstOrDefault();
string nonce = Session["Nonce"] as string;
if(admin == null || string.IsNullOrWhiteSpace(nonce))
{
return RedirectToAction("Index", "Posts");
}
string computedHash;
using (SHA256 sha256 = SHA256.Create()) //sha256
{
byte[] hashInput = Encoding.ASCII.GetBytes(admin.Password + nonce);
byte[] hashData = sha256.ComputeHash(hashInput);
StringBuilder stringBuidler= new StringBuilder();
foreach(byte value in hashData)
{
stringBuidler.AppendFormat("{0:X2}", value);
}
computedHash = stringBuidler.ToString();
}
Session ["IsAdmin"]= (computedHash.ToLower() == hash.ToLower());
return RedirectToAction("Index","Posts");
}
public ActionResult Logout()
{
Session["Nonce"] = null;
Session["IsAdmin"] = null;
return RedirectToAction("Index", "Posts");
}
public ActionResult Index()
{
return View();
}
EDIT: getPasswordHash when i right click on its definition, it tell me "failed" either because the caret is already at the definition or because an explicit definition could not be found