我在ASP.NET核心应用程序中有一个登录页面,弹出以向用户电子邮件发送密码重置电子邮件(仅使用身份):
<div class="login-body">
<div class="container">
<form asp-controller="Account" asp-action="Login" asp-route-returnurl="@ViewData["ReturnUrl"]" method="post" class="form-signin">
<h2 class="form-signin-heading">@Localizer["Sign In"]</h2>
<div class="login-wrap">
<div class="user-login-info">
<input asp-for="Email" type="email" class="form-control" placeholder="@Localizer["Email"]" autofocus/>
<span asp-validation-for="Email" class="text-danger"></span>
<input asp-for="Password" type="password" class="form-control" placeholder="@Localizer["Password"]"/>
<span asp-validation-for="Password" class="text-danger"></span>
</div>
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<label class="checkbox">
<input asp-for="RememberMe"/> Remember me
<span class="pull-right">
<a data-toggle="modal" href="#forgotPassModal">Reset Password</a>
</span>
</label>
<button class="btn btn-lg btn-login btn-block" type="submit">@Localizer["Sign In"]</button>
<div class="registration">
@Localizer["No Account"]
<a asp-area="" asp-controller="Account" asp-action="Register">@Localizer["Create Account"]</a>
</div>
</div>
<!-- Modal -->
<div aria-hidden="true" aria-labelledby="myModalLabel" role="dialog" tabindex="-1" id="forgotPassModal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Reset Password</h4>
</div>
<div class="modal-body">
<p>Enter your e-mail address below to reset your password.</p>
<input type="text" name="email" placeholder="Email" autocomplete="off" class="form-control placeholder-no-fix">
</div>
<div class="modal-footer">
<button data-dismiss="modal" class="btn btn-default" type="button">Cancel</button>
<button class="btn btn-success" asp-controller="Account" asp-action="ForgotPassword" method="post">Submit</button>
@*<input type="button" class="btn btn-success" asp-controller="Account" asp-action="ForgotPassword" formmethod="post">Submit<input>*@
</div>
</div>
</div>
</div>
<!-- modal -->
</form>
</div>
当我输入电子邮件地址时,它会做正确的事情,发送带有代码的电子邮件,但它会将我重定向到一个单独的视图,说明要检查您的电子邮件。
是否可以将消息返回到弹出窗口或登录页面说同样的事情?
这是ForgotPassword Action:
public async Task<IActionResult> ForgotPassword(ForgotPasswordViewModel model)
{
if (ModelState.IsValid)
{
var user = await UserManager.FindByEmailAsync(model.Email);
if (user == null || !(await UserManager.IsEmailConfirmedAsync(user)))
{
return View("ForgotPasswordConfirmation");
}
var code = await UserManager.GeneratePasswordResetTokenAsync(user);
var callbackUrl = Url.Action(nameof(ResetPassword), "Account", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme);
await _emailSender.SendEmailAsync(model.Email, "Reset Password",
$"Please reset your password by clicking here: <a href='{callbackUrl}'>link</a>");
return View("ForgotPasswordConfirmation");
}
return View(model);
}
答案 0 :(得分:2)
您可以使用的是TempData。 TempData可以保存一个额外请求的数据,仅用于一个额外的请求。因此,您可以将此信息存储在TempData中。它适用于您的用例。它可能看起来像这样。
public IActionResult Login()
{
var loginModel = new LoginModel();
loginModel.ForgotPassWordModel = (ForgotPassWordModel) TempData["ForgotPassword"];
return View(loginModel);
}
public IActionResult ForgotPassword()
{
if (valid)
{
TempData["ForgotPassword"] = new ForgotPassWordModel() {callbackUrl = "Go to this"};
return RedirectToAction("Login");
}
return View();
}
这是我的示例LoginModel和我的ForgottonPasswordModel。当然,你的会更复杂。
public class ForgotPassWordModel
{
public string callbackUrl;
}
public class LoginModel
{
public ForgotPassWordModel ForgotPassWordModel { get; set; }
}
当有人向ForgotPassword发出请求时,如果它有效,则将结果存储在TempData中并重定向回Login。现在您可以读取您在上一个请求中设置的TempData [“ForgotPassword”]。在我的示例中,我将数据放入LoginModel中。如果没有tempData,则为null。现在,在您的视图中,您可以检查此ForgotPassword属性。
@if (Model.ForgotPassWordModel != null)
{
// Show message/modal
}
关于TempData的一个好处是,当你设置数据时,它只会再保留一个额外的请求,所以它适用于这样的情况。