我有一个登录页面,用户输入他们的电子邮件和密码,但是当他们输入错误的密码时,我想显示警报或类似的文本(您输入了无效的密码)。我已经检查过Checkpass
是否不为空,请执行某项操作,否则我不知道该怎么做。请问有人可以帮助我或指出正确的方向吗?
提前感谢:)
控制器:
[HttpPost]
public JsonResult Login(string Mail, string pass)
{
var hashedPass = PasswordSecurity.PasswordStorage.CreateHash(pass);
using (DbNameSpace db = new DbNameSpace())
{
var query = from cbr in db.Contact_Business_Relation
join c in db.Contact on cbr.Contact_No_ equals c.Company_No_
join sa in db.Sales_Header on cbr.No_ equals sa.Sell_to_Customer_No_
join px in db.PX2 on c.E_Mail equals px.Email_ID
where c.E_Mail == Mail.ToLower()
select new
{
Mail = c.E_Mail,
pass = px.PS,
};
var user = query.FirstOrDefault();
var CheckPass = PasswordSecurity.PasswordStorage.VerifyPassword(pass, user.pass);
if (user != null && CheckPass) //Checkpassword
{
Session["Email"] = user.Mail.ToString();
}
else {
// ??
}
return Json(user, JsonRequestBehavior.AllowGet);
}
}
JavaScript:
<script>
$(document).ready(function () {
$("#login").click(function (e) {
var email = $("input[name=Mail]").val();
var password = $("input[name=pass]").val();
e.preventDefault();
$.ajax({
type: "POST",
dataType: "json",
url: '@Url.Action("Login", "Account")',
data: { Mail: email, pass: password },
success: function (status) {
if (status) {
window.location.href = "/Account/Index";
}
}
});
});
});
</script>
查看:
<form autocomplete="on" class="login100-form validate-form">
<div>
<label>E-mail</label>
<div class="wrap-input100 validate-input" data-validate="Valid email is required: ex@abc.xyz">
<input class="input100" type="email" id="Email" name="Mail" placeholder="E-mail">
</div>
<label>Password</label>
<div class="wrap-input100 validate-input" data-validate="Password is required">
<input class="input100" type="password" id="Password" name="pass" placeholder="Kodeord">
</div>
<div id="invalidpassword"></div>
<div class="container-login100-form-btn">
<button id="login" class="login100-form-btn">
Log in
</button>
</div>
</form>
答案 0 :(得分:1)
查看此jquery教程。我个人将使用Fail和Done回调方法。修改您的c#控制器以返回不同的HTTP状态代码。当他们传递正确的用户名和密码时,请使用HTTP 200。当他们通过错误的密码时使用HTTP 400,这将触发Fail()回调并允许您警告失败。
https://learn.jquery.com/ajax/jquery-ajax-methods/
// Using the core $.ajax() method
$.ajax({
// The URL for the request
url: "post.php",
// The data to send (will be converted to a query string)
data: {
id: 123
},
// Whether this is a POST or GET request
type: "GET",
// The type of data we expect back
dataType : "json",
})
// Code to run if the request succeeds (is done);
// The response is passed to the function
.done(function( json ) {
$( "<h1>" ).text( json.title ).appendTo( "body" );
$( "<div class=\"content\">").html( json.html ).appendTo( "body" );
})
// Code to run if the request fails; the raw request and
// status codes are passed to the function
.fail(function( xhr, status, errorThrown )** {
alert( "Sorry, there was a problem!" );
console.log( "Error: " + errorThrown );
console.log( "Status: " + status );
console.dir( xhr );
})
// Code to run regardless of success or failure;
.always(function( xhr, status ) {
alert( "The request is complete!" );
});
答案 1 :(得分:0)
通常要做的是将响应状态设置为401,并忽略将在成功响应后发送的常规数据有效负载。然后由客户端代码识别错误状态并做出适当响应(例如通过显示警报并保留在名称/密码表单中);此客户端行为将在$.ajax
调用中指定。
答案 2 :(得分:0)
许多选项,
最简单的一种: 在执行操作时,您还可以返回JSON:
return Json(new { Error = "Username & password does not match." });
然后在您的成功处理程序中,您可以进行检查:
$.ajax({
...
success: function (result) {
if (result && result.error) {
//show your error in your way
return;
}
...
}
});
另一种方法是引发错误或返回Unauthorized();
:
if (user == null || !passcheck)
{
throw new Exception("Username & password does not match.");
//return Unauthorized();
}
然后您将需要$ .ajax错误处理程序
$.ajax({
...
error: function (xhr, textStatus, err) {
//handle error from 3 parameters.
debugger;
},
...
});
答案 3 :(得分:0)
这是一个例子。希望对您有帮助,我的朋友。
[HttpPost]
public JsonResult Login(string Mail, string pass)
{
var status = true;
var hashedPass = PasswordSecurity.PasswordStorage.CreateHash(pass);
using (DbNameSpace db = new DbNameSpace())
{
var query = from cbr in db.Contact_Business_Relation
join c in db.Contact on cbr.Contact_No_ equals c.Company_No_
join sa in db.Sales_Header on cbr.No_ equals sa.Sell_to_Customer_No_
join px in db.PX2 on c.E_Mail equals px.Email_ID
where c.E_Mail == Mail.ToLower()
select new
{
Mail = c.E_Mail,
pass = px.PS,
};
var user = query.FirstOrDefault();
var CheckPass = PasswordSecurity.PasswordStorage.VerifyPassword(pass, user.pass);
if (user != null && CheckPass) //Checkpassword
{
Session["Email"] = user.Mail.ToString();
}
else {
status = false;
}
return Json(status, JsonRequestBehavior.AllowGet);
}
}
在Java语言中:
<script>
$(document).ready(function () {
$("#login").click(function (e) {
var email = $("input[name=Mail]").val();
var password = $("input[name=pass]").val();
e.preventDefault();
$.ajax({
type: "POST",
dataType: "json",
url: '@Url.Action("Login", "Account")',
data: { Mail: email, pass: password },
success: function (status) {
if (status) {
window.location.href = "/Account/Index";
}else{
$('#invalidpassword').html('You have entered wrong password!');
}
}
});
});
});
</script>