ASP.NET Identity 3.0 上 IIdentityMessageService 的等价物是什么?
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 Identity 似乎没有更多的电子邮件服务。您只需定义自己的接口。 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;
}
在 ASP.NET Identity 2.X 上,我们可以通过 Microsoft.AspNet.Identity.Core
库中可用的 IIdentityMessageService
接口配置通知基础结构,该库未升级到 3.0 版。
在 ASP.NET Identity 3.0 上配置消息基础结构的做法是什么?
Asp.Net Identity 似乎没有更多的电子邮件服务。您只需定义自己的接口。 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;
}