What is the equivalent for IIdentityMessageService on ASP.NET Identity 3.0?

℡╲_俬逩灬. 提交于 2019-12-23 15:44:57

问题


On ASP.NET Identity 2.X we could configure a notification infrastructure trhough the IIdentityMessageService interface available at the Microsoft.AspNet.Identity.Core library, which was not upgraded to version 3.0.

What are the practices for configuring messaging infrastructure on ASP.NET Identity 3.0?


回答1:


It seems that there is no more email service plugged to the Asp.Net Identity. You just define your own interface. The role of ASP.NET identity is to generate and validate the e-mail confirmation token and the password reset token.

public interface IEmailService
{
    Task SendAsync(string to, string subject, string body);
}

To send an e-mail to verify account

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 is an IUrlHelper. It could be the property Controller.Url or a new instance generated by constructor injection.

Then, to confirm the e-mail

[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;
}

Forgot password

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;
}


来源:https://stackoverflow.com/questions/32540273/what-is-the-equivalent-for-iidentitymessageservice-on-asp-net-identity-3-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!