當新建一個core專案后,使用identity基架后,確認郵件出現了錯誤,并不能正常使用,
建立檔案在這里
https://docs.microsoft.com/zh-cn/aspnet/core/security/authentication/scaffold-identity?view=aspnetcore-3.1&tabs=visual-studio#scaffold-identity-into-an-empty-project
參考后發現,并沒有實作這個介面,需要自己完成,這是官方檔案,可是官方第三方key
https://docs.microsoft.com/zh-cn/aspnet/core/security/authentication/accconfirm?view=aspnetcore-3.1&tabs=visual-studio#require-email-confirmation
下面,使用一個自己的習慣吧,就是用自己的郵箱發送內容給用戶,
和官方檔案一樣,建立一個EmailSender
兄弟 們接代碼:
public class EmailSender : IEmailSender { public async Task SendEmailAsync(string email, string subject, string message) { // 設定郵件內容 var mail = new MailMessage( new MailAddress("[email protected]", "王彬"), new MailAddress(email) ); mail.Subject = subject; mail.Body = message; mail.IsBodyHtml = true; mail.BodyEncoding = Encoding.UTF8; mail.Priority = MailPriority.High;//郵件優先級 // 設定SMTP服務器 var smtp = new SmtpClient("smtp.163.com", 25); smtp.UseDefaultCredentials = false; smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "*******“; smtp.DeliveryMethod = SmtpDeliveryMethod.Network; await smtp.SendMailAsync(mail); } }
然后,我們將以下代碼添加到Startup.cs檔案的 ConfigureServices 方法中:
- 將
EmailSender添加為暫時性服務, - 注冊
AuthMessageSenderOptions配置實體,
services.AddTransient<IEmailSender, EmailSender>();
好了,剩下的就是官方一至內容了,identity還是很方便的,
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/83017.html
標籤:.NET Core
