![](/img/trans.png)
[英]ASP.NET Identity UserManager IIdentityMessageService pass extra parameters to SendAsync
[英]What is the equivalent for IIdentityMessageService on ASP.NET Identity 3.0?
在ASP.NET Identity 2.X上,我們可以通過Microsoft.AspNet.Identity.Core
庫中提供的IIdentityMessageService
接口配置通知基礎結構,該庫未升級到3.0版。
在ASP.NET Identity 3.0上配置消息傳遞基礎結構的做法是什么?
似乎沒有更多的電子郵件服務插入Asp.Net標識。 您只需定義自己的界面即可。 ASP.NET標識的作用是生成並驗證電子郵件確認令牌和密碼重置令牌。
public interface IEmailService
{
Task SendAsync(string to, string subject, string body);
}
private async Task SendEmailConfirmation(User user)
{
string token = await this._userManager.GenerateEmailConfirmationTokenAsync(user);
string callbackUrl = this._urlHelper.Action("EmailConfirmed", "Account", new ConfirmTokenViewModel(user.Id, token), protocol: this._contextAccessor.HttpContext.Request.Scheme);
await this._emailService.SendAsync(to: user.Email,
subject: "Confirm your account",
body: "Please confirm your e-mail by clicking this link: <a href=\"" + callbackUrl + "\">link</a>");
}
OBS:this._urlHelper是一個IUrlHelper。 它可以是屬性Controller.Url或構造函數注入生成的新實例。
然后,確認電子郵件
[HttpGet]
[AllowAnonymous]
public async Task<IActionResult> EmailConfirmed(ConfirmTokenViewModel model)
{
if (!this.ModelState.IsValid)
return View("Error");
bool succeeded = await this._accountsManager.ConfirmEmail(model.UserId, model.Token);
return succeeded ? View() : View("Error");
}
public async Task<bool> ConfirmEmail(string userId, string token)
{
User user = await _userManager.FindByIdAsync(userId);
if (user != null)
{
var result = await _userManager.ConfirmEmailAsync(user, token);
return result.Succeeded;
}
return false;
}
public async Task GeneratePasswordTokenAndSendEmailAsync(string email)
{
var user = await _userManager.FindByNameAsync(email);
if (user != null && await _userManager.IsEmailConfirmedAsync(user))
{
string token = await _userManager.GeneratePasswordResetTokenAsync(user);
string callbackUrl = this._urlHelper.Action("ResetPassword", "Account", new ConfirmTokenViewModel(user.Id, token), protocol: this._contextAccessor.HttpContext.Request.Scheme);
await this._emailService.SendAsync(
to: user.Email,
subject: "Reset password",
body: "Reset your password by clicking this link: <a href=\"" + callbackUrl + "\">link</a>"
});
}
}
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<IActionResult> ResetPassword(ResetPasswordViewModel model)
{
if (ModelState.IsValid)
{
IdentityResult result = await this._accountsManager.ResetPasswordAsync(model);
if (result.Succeeded)
{
return RedirectToAction(nameof(ResetPasswordConfirmation), "Account");
}
else
ModelState.AddModelErrors(result.Errors);
}
// If we got this far, something failed, redisplay form
return View(model);
}
public async Task<IdentityResult> ResetPasswordAsync(ResetPasswordViewModel model)
{
IdentityResult result = IdentityResult.Success;
if (model != null && !string.IsNullOrWhiteSpace(model.UserId))
{
User user = await _userManager.FindByIdAsync(model.UserId);
if (user != null)
result = await _userManager.ResetPasswordAsync(user, model.Token, model.Password);
}
return result;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.