0
我在控制器中通過郵件重置用戶密碼的方法。JsonResult返回HTML響應
public JsonResult RecoverPasswordByEmail(string mail)
{
MembershipUser member = Membership.GetUser(Membership.GetUserNameByEmail(mail));
string newPassword = System.Web.Security.Membership.GeneratePassword(14, 0);
member.UnlockUser();
if (!member.ChangePassword(member.ResetPassword(), newPassword))
{
return Json(new { Resultado = false, Excepcion = "Couldn't change password" }, JsonRequestBehavior.AllowGet);
}
System.Net.Mail.MailAddress from = new System.Net.Mail.MailAddress("[email protected]");
System.Net.Mail.MailAddress to = new System.Net.Mail.MailAddress(mail);
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(from, to);
message.Subject = "Forgot pass";
if (member.IsLockedOut)
{
message.Body = "You're locked";
}
else
{
message.Body = "New password: " + newPassword;
}
var client = new System.Net.Mail.SmtpClient("my.smtpserver.com", 587)
{
Credentials = new System.Net.NetworkCredential("[email protected]", "12345"),
EnableSsl = true
};
try
{
client.Send(message);
}
catch (System.Net.Mail.SmtpException ex)
{
return Json(new { Resultado = false, Excepcion = ex.Message }, JsonRequestBehavior.AllowGet);
}
return Json(new { Resultado = true }, JsonRequestBehavior.AllowGet);
}
我使用jQuery從一個對話框中的按鈕做一個ajax請求從登錄視圖。
奇怪的是我使用了另一種控制器方法RecoverPassword,它可以通過用戶名和用戶名執行相同的操作。使用螢火蟲我看到RecoverPassword完成這項工作,並返回一個JSON與結果,但RecoverPasswordByEmail迴應一個大的HTML文件。
的HTML的重要組成部分:
<div id="dialog">
<h2>Retrieve Password</h2>
@Html.Label("Mail:")<br/>
@Html.TextBox("txtMail")
<div id="loading">
<br/><img class="displayed" src="@Url.Content("~/Content/Images/Ajax/ajax-loader3.gif")" alt="loading" /><br/>
@Html.Label("Error")
</div>
<br/><br/><input class="button" id="btnSendMail" type="submit" value="Get new password" />
<div>
<a href="#" id="showDialog">Recuperar contraseña</a>
</div>
</div>
而且JS:
$(文件)。就緒(函數(){
var requestMail;
$('#btnSendMail').button();
$('#loading').hide();
$("label[for=Error]").text("");
$('#btnSendMail').click(function (event) {
event.preventDefault();
var mail = $("#txtMail").val();
if (mail.length > 0) {
if (requestMail && requestMail .readystate != 4) {
requestMail .abort();
}
$('#loading').show();
$('input[id="btnSendMail"]').attr('disabled', 'disabled');
requestCorreo = $.ajax({
url: '/Users/RecoverPasswordByEmail',
type: 'POST',
dataType: 'json',
contentType: "application/json; charset=utf-8",
timeout: 8000,
data: { Email: mail },
success: function (response) {
if (response.Result) {
$("label[for=Error]").text('New password has been sent to: ' + mail);
}
else {
alert(response.Result + ' ' + response.Exception);
}
},
error: function (xhr, textStatus, thrownError) {
if (textStatus === "timeout") {
alert("got timeout");
}
else {
alert(xhr.status + ' ' + textStatus + ' ' + thrownError);
}
},
complete: function() {
$('#loading').hide();
$('input[id="btnSendMail"]').removeAttr('disabled');
}
});
}
else {
$("label[for=Error]").text("Insert a valid email");
}
requestCorreo.done(function (msg) {
alert(msg);
});
requestCorreo.fail(function (jqXHR, textStatus) {
alert("Request failed: " + textStatus + " " + jqXHR.responseText);
});
});
});
我更新了這篇文章,只是忽略了js中的額外代碼,裏面有一大堆代碼,所以我只是複製了我認爲它涉及的問題。 – TheGman
我假設你放在帖子中的HTML是你的視圖文件中的剃刀 - 但是你的Ajax請求返回了什麼HTML? – stevehayter
我認爲這將是Forms身份驗證實現的登錄重定向,它將返回Login視圖作爲響應。現在我不知道我怎麼能通過這個機制。 – TheGman