【发布时间】:2016-01-19 19:06:00
【问题描述】:
我正在尝试使用以下方法获取登录表单。我正在使用 JQuery 版本 1.12.0 和 MVC5。我只是创建了一个帐户控制器,当管理员登录时,他/她可以执行 CRUD 功能,其他用户只能看到帖子和 cmets 等等,但是登录页面不起作用意味着当我按下登录时没有任何反应。页面不刷新。它的行为就像我没有点击一样 这是登录视图
@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>
这是登录js文件代码
function getPasswordHash (passwordElement, nonceElement, hashElement)
{
var password = $('#' + passwordElement).attr('value');
var nonce = $('#' + nonceElement).attr('value');
$('#' + hashElement).attr('value', $.sha256(password + nonce));
$('#' + passwordElement).attr('value', '');
}
这是用于登录的帐户控制器
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();
}
编辑:当我右键单击它的定义时,它告诉我“失败” 要么是因为插入符号已经在定义中,要么是因为找不到显式定义
【问题讨论】:
-
你为什么首先计算一个哈希客户端?为什么不直接取出 JavaScript 并使用普通的
submit按钮? -
先生,我是初学者,正在观看视频进行练习。任何帮助将不胜感激提前感谢
标签: javascript c# jquery asp.net-mvc login