gpt4 book ai didi

c# - ASP.NET Identity UserManager IIdentityMessageService 将额外参数传递给 SendAsync

转载 作者:行者123 更新时间:2023-11-30 17:35:25 24 4
gpt4 key购买 nike

我已经通过扩展 IIdentityMessageService 成功地为 UserManager 创建了自定义电子邮件服务提供商:

public class ExchangeEmailService : IIdentityMessageService
{
public async Task SendAsync(IdentityMessage message)
{
using (var client = new SmtpClient())
{
client.Host = "mail.example.com";
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential(@"noreply", "P@ssw0rd");

var from = new MailAddress("no-reply@example.com");
var to = new MailAddress(message.Destination);

var mailMessage = new MailMessage(from, to)
{
Subject = message.Subject,
Body = message.Body,
IsBodyHtml = true
};
await client.SendMailAsync(mailMessage);
}
}
}

UserManager 的 Create 中,我正在创建我的服务的新实例并将其分配给 EmailService:

userManager.EmailService = new ExchangeEmailService();

一切正常,但我需要根据调用的位置从不同的电子邮件地址发送电子邮件,不幸的是 UserManager.SendEmailAsync 不允许我传递除 userId 之外的任何内容,主题和正文。

理想情况下,我希望能够在调用 SendEmailAcync 或枚举值时指定电子邮件地址。

我在 UserManager source 中搜索过我考虑过创建自定义 SendEmailAsync 实现,但我必须更改多个位置 - IIdentityMessageService、UserManager。

我知道我可以创建新界面并在其上构建我的电子邮件发送类,但我希望将更改保持在最低限度。

我想得到的:
我想要包含邮箱名称的枚举:

public enum Mailboxes
{
Noreply = 0,
Service = 1,
Contact = 2
}

并能够使用该附加参数调用 SendEmailAsync:

await UserManager.SendEmailAsync(user.Id, "Account created", "Nice email content.", Mailboxes.Noreply );

我知道我可以设置不同的标题并使用相同的电子邮件地址,但这是我的要求。

我该怎么做?

最佳答案

一个 hack 是创建一个模型来存储您想要发送到服务的内容

public class MessageBody {
public string Body { get; set; }
public MailBoxes Source { get; set; }
}

然后将其序列化(可能是 JSON)作为 SendEmailAsync 方法调用的 body 参数。

var message = new MessageBody {
Body = "Nice email content.",
Source = Mailboxes.Noreply
};

var body = JsonConvert.SerializeObject(message);

await UserManager.SendEmailAsync(user.Id, "Account created", body);

然后该服务将反序列化模型,使用自定义信息构建电子邮件,然后发送电子邮件。

public class ExchangeEmailService : IIdentityMessageService {
readonly IMailBoxProvider provider;

public ExchangeEmailService(IMailBoxProvider provider) {
this.provider = provider;
}

public async Task SendAsync(IdentityMessage message) {
using (var client = new SmtpClient()) {
client.Host = "mail.example.com";
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential(@"noreply", "P@ssw0rd");

//Get the body and from address
var fromEmailAddress = "default-email@example.com";
var body = message.Body;
try {
var msg = JsonConvert.DeserializeObject<MessageBody>(body);
if(msg != null) {
body = msg.Body;
fromEmailAddress = provider.GetMailbox(msg.Source);
}
} catch { }

var from = new MailAddress(fromEmailAddress);
var to = new MailAddress(message.Destination);

var mailMessage = new MailMessage(from, to)
{
Subject = message.Subject,
Body = body,
IsBodyHtml = true
};
await client.SendMailAsync(mailMessage);
}
}
}

只需确保将其分配给 UserManager.EmailService:

userManager.EmailService = new ExchangeEmailService(new MailBoxProvider());

关于c# - ASP.NET Identity UserManager IIdentityMessageService 将额外参数传递给 SendAsync,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41222201/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com